title: Compress image in tiff viewer
body: <p>I have created Tiff file viewer for my web application and it's working great. But the problem is there are some big images (4000 X 2000 - 5+ MB) which I need to display in it's original size. </p>
  
  <p>Is it possible to compress the size of the image and then send the stream to Response Stream on the ashx page?</p>
  
  <p>Note that all my images are scanned black and white images. However for precaution I would like to check whether compression algorithm specific to black &amp; white image be applied to the  image or not.</p>
  
  <p>Look the below code of TiffHelper class</p>
  
  <pre><code>using System;
  using System.Drawing;
  using System.Drawing.Imaging;
  using System.IO;
  using System.Drawing.Drawing2D;
  
  public class TiffHelper
  {
      public static int GetPageCount(String filePath)
      {
          int pageCount = 0;
          try
          {
              using (Image img = Image.FromStream(
                                  new FileStream(filePath,
                                   FileMode.Open,
                                   FileAccess.Read,
                                   FileShare.ReadWrite)
                             ))
              {
                  pageCount = img.GetFrameCount(FrameDimension.Page);
              }
          }
          catch (Exception ex)
          {
              CException.SuppressException(ex);
              pageCount = -1;
          }
  
          return pageCount;
      }
  
      public static Image GetTiffImage(String sourceFile, int pageNumber, int thumbnailSize, int degree)
      {
          MemoryStream ms = null;
          Image sourceImage = null;
          Image returnImage = null;
  
          try
          {
              sourceImage = Image.FromStream(
                                  new FileStream(sourceFile,
                                   FileMode.Open,
                                   FileAccess.Read,
                                   FileShare.ReadWrite)
                             );
              ms = new MemoryStream();
              FrameDimension FrDim = new FrameDimension(sourceImage.FrameDimensionsList[0]);
              sourceImage.SelectActiveFrame(FrDim, pageNumber - 1);
  
              sourceImage.Save(ms, ImageFormat.Tiff);
  
              /*
              EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, 90);
              ImageCodecInfo tiffCodec = GetEncoderInfo("image/tiff");
              EncoderParameters encoderParams = new EncoderParameters(1);
              encoderParams.Param[0] = qualityParam;
  
              sourceImage.Save(ms, tiffCodec, encoderParams); 
              */
  
              /*
              EncoderParameters iparams = new EncoderParameters(2);
              iparams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
              iparams.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
               * 
              sourceImage.Save(ms, GetEncoderInfo("image/tiff"), iparams);
              */
  
              int height = sourceImage.Height;
              int width = sourceImage.Width;
  
              if (thumbnailSize &gt; 0)
              {
                  if (sourceImage.Width &lt; sourceImage.Height)
                  {
                      width = thumbnailSize;
                      height = sourceImage.Height * thumbnailSize / sourceImage.Width;
                  }
                  else
                  {
                      width = sourceImage.Width * thumbnailSize / sourceImage.Height;
                      height = thumbnailSize;
                  }
              }
  
              //returnImage = ResizeImage(Image.FromStream(ms), new Size(width, height));
  
              returnImage = Image.FromStream(ms).GetThumbnailImage(width, height, null, IntPtr.Zero);
              returnImage.RotateFlip(GetFlipType(degree));
          }
          catch (Exception ex)
          {
              CException.SuppressException(ex);
          }
          finally
          {
              ms.Dispose();
              sourceImage.Dispose();
          }
  
          return returnImage;
      }
  
      private static Image ResizeImage(Image imgToResize, Size size)
      {
          int sourceWidth = imgToResize.Width;
          int sourceHeight = imgToResize.Height;
  
          float nPercent = 0;
          float nPercentW = 0;
          float nPercentH = 0;
  
          nPercentW = ((float)size.Width / (float)sourceWidth);
          nPercentH = ((float)size.Height / (float)sourceHeight);
  
          if (nPercentH &lt; nPercentW)
              nPercent = nPercentH;
          else
              nPercent = nPercentW;
  
          int destWidth = (int)(sourceWidth * nPercent);
          int destHeight = (int)(sourceHeight * nPercent);
  
          Bitmap b = new Bitmap(destWidth, destHeight);
          Graphics g = Graphics.FromImage((Image)b);
  
          g.InterpolationMode = InterpolationMode.HighQualityBicubic;
          g.SmoothingMode = SmoothingMode.HighSpeed;
          g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
          g.CompositingQuality = CompositingQuality.HighQuality;
  
          g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
          g.Dispose();
  
          return (Image)b;
      }
  
      private static ImageCodecInfo GetEncoderInfo(string mimeType)
      {
          ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
  
          for (int i = 0; i &lt; codecs.Length; i++)
          {
              if (codecs[i].MimeType == mimeType)
              {
                  return codecs[i];
              }
          }
  
          return null;
      }
  
      private static RotateFlipType GetFlipType(int degree)
      {
          switch (degree)
          {
              case 90:
                  return RotateFlipType.Rotate90FlipNone;
              case 180:
                  return RotateFlipType.Rotate180FlipNone;
              case 270:
                  return RotateFlipType.Rotate270FlipNone;
              case -90:
                  return RotateFlipType.Rotate90FlipXY;
              case -180:
                  return RotateFlipType.Rotate180FlipNone;
              case -270:
                  return RotateFlipType.Rotate270FlipXY;
              default:
                  return RotateFlipType.RotateNoneFlipNone;
          }
      }
  }
  </code></pre>