using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.IO; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace TiffTest { public partial class Form4 : Form { public Form4() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string sourcePath = @"C:\Users\pentaven\Desktop\Test Images\1.TIF";//.tif";FLDADE string DistPath = @"C:\Users\pentaven\Desktop\Test Images\Test.tif"; OpenFileDialog fd = new OpenFileDialog(); fd.Filter = "*.tiff|*.tif"; fd.InitialDirectory = @"C:\Users\pentaven\Desktop\Test Images"; fd.Multiselect = false; if (fd.ShowDialog() == DialogResult.OK) { sourcePath = fd.FileName; } else { return; } if (string.IsNullOrEmpty(sourcePath)) return; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); label1.Text = "Time Spent : "; FileStream fs = File.Open(sourcePath, FileMode.Open, FileAccess.Read); MemoryStream ms = ResizeImage(fs, checkBox1.Checked); ms.Position = 0; FileStream outFile = File.OpenWrite(DistPath); outFile.Write(ms.GetBuffer(), 0, (int)ms.Length); outFile.Close(); fs.Close(); sw.Stop(); label1.Text = "Time Spent : " + sw.Elapsed.TotalSeconds.ToString(); } public MemoryStream ResizeImage(Stream imageStream, bool Compress) { MemoryStream ms = null; try { imageStream.Position = 0; Bitmap srcBmp = Bitmap.FromStream(imageStream) as Bitmap; int totalPages = srcBmp.GetFrameCount(FrameDimension.Page); ImageCodecInfo ici = GetTiffCodec(); EncoderParameters ep = GetBWEncoderParams(8); int currWidth = srcBmp.Width; int currHeight = srcBmp.Height; float pageFitWidth = 1800; float pageFitHeight = 2200; if ((currWidth != pageFitWidth) || (currHeight != pageFitHeight)) { Bitmap destFile = null; int NewW = (int)pageFitWidth, NewH = (int)pageFitHeight; PixelFormat NewPixelFormat = srcBmp.PixelFormat; if (NewPixelFormat == PixelFormat.Format1bppIndexed || NewPixelFormat == PixelFormat.Format4bppIndexed || NewPixelFormat == PixelFormat.Format8bppIndexed || NewPixelFormat == PixelFormat.Format16bppGrayScale) NewPixelFormat = PixelFormat.Format24bppRgb; ms = new MemoryStream(); for (int currentPage = 0; currentPage < totalPages; currentPage++) { srcBmp.SelectActiveFrame(FrameDimension.Page, currentPage); Bitmap temp = new Bitmap(NewW, NewH, NewPixelFormat); Graphics g = Graphics.FromImage(temp); g.DrawImage(srcBmp, new Rectangle(0, 0, temp.Width, temp.Height), 0, 0, srcBmp.Width, srcBmp.Height, GraphicsUnit.Pixel); g.Dispose(); if (currentPage == 0) { if (Compress) temp = ConvertToFormat1bppIndexedFrom32bppPArgb(temp); destFile = temp; destFile.Save(ms, ici, ep); } else { if (Compress) temp = ConvertToFormat1bppIndexedFrom32bppPArgb(temp); ep.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); destFile.SaveAdd(temp, ep); } if (currentPage == totalPages - 1) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush); destFile.SaveAdd(ep); } } destFile.Dispose(); } } catch (Exception) { ms = null; } return ms; } private ImageCodecInfo GetTiffCodec() { foreach (ImageCodecInfo ici in ImageCodecInfo.GetImageEncoders()) { if (ici.MimeType == "image/tiff") return ici; } return null; } private EncoderParameters GetBWEncoderParams(int colorDepth) { EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionLZW); //ep.Param[2] = new EncoderParameter(Encoder.ColorDepth, (long)colorDepth); return ep; } private Bitmap ConvertToFormat1bppIndexedFrom32bppPArgb(Bitmap page) { Bitmap tifpage = page; BitmapData bmdo = tifpage.LockBits(new Rectangle(0, 0, tifpage.Width, tifpage.Height), ImageLockMode.ReadOnly, tifpage.PixelFormat); //and the new 1bpp bitmap Bitmap bm = new Bitmap(tifpage.Width, tifpage.Height, PixelFormat.Format1bppIndexed); BitmapData bmdn = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed); //scan through the pixels Y by X for (int y = 0; y < tifpage.Height - 1; y++) { for (int x = 0; x < tifpage.Width - 1; x++) { //generate the address of the colour pixel int index = y * bmdo.Stride + x * 4; //check its brightness if (Color.FromArgb(Marshal.ReadByte(bmdo.Scan0, index + 2), Marshal.ReadByte(bmdo.Scan0, index + 1), Marshal.ReadByte(bmdo.Scan0, index)).GetBrightness() > 0.5F) SetIndexedPixel(x, y, bmdn, true); //set it if its bright.; } } //tidy up bm.UnlockBits(bmdn); tifpage.UnlockBits(bmdo); tifpage.Dispose(); page = (Bitmap)bm; return page; } private void SetIndexedPixel(int x, int y, BitmapData bmd, Boolean pixel) { int index = y * bmd.Stride + (x >> 3); Byte p = Marshal.ReadByte(bmd.Scan0, index); byte mask = (byte)(0x80 >> (x & 0x7)); if (pixel) p = (byte)(p | mask); else p = (byte)(p & Convert.ToByte(Math.Pow(mask, 0xff))); Marshal.WriteByte(bmd.Scan0, index, p); } } }