Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tiff file compression with C#

Tags:

c#

tiff

I have a tiff file which during original creation and saving has compression type "LZW".

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(800, 1000); 
Graphics g = Graphics.FromImage(bitmap);
fileName = saveDirectory + id + ".tif";
bitmap.Save(fileName, ImageFormat.Tiff);

So I am trying to change its compression to CCIT:-

Bitmap myBitmap;
myBitmap = new Bitmap(fileName);
ImageCodecInfo myImageCodecInfo;
myImageCodecInfo = GetEncoderInfo("image/tiff");
System.Drawing.Imaging.Encoder myEncoder;
myEncoder = System.Drawing.Imaging.Encoder.Compression;
EncoderParameters myEncoderParameters;
myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter;      
myEncoderParameter = new EncoderParameter(myEncoder,(long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save(new_fileName);

    private static ImageCodecInfo GetEncoderInfo(String mimeType)
    {
      int j;
      ImageCodecInfo[] encoders;
      encoders = ImageCodecInfo.GetImageEncoders();
      for (j = 0; j < encoders.Length; ++j)
        {
            if (encoders[j].MimeType == mimeType)
            return encoders[j];
        }
      return null;
    }

However, after running through the process, my file properties still says that it is of the "LZW compression" type.

Can anyone explain to me where I am going wrong with this?

like image 923
Philo Avatar asked Jan 16 '14 17:01

Philo


People also ask

Does TIFF allow compression?

The Tagged Image File Format (TIFF) is widely popular, and is particularly used in document imaging. It can support a number of compression types: Packbits – Created by Apple, this lossless compression type is used for run-length encoding (RLE). Baseline TIFF readers must support this compression.

What is the best compression for TIFF?

Both LZW and ZIP will give good results. Use either with confidence. For 16-bit TIFF files, use ZIP.

Does TIF lose quality?

TIFF files are much larger than JPEGs, but they're also lossless. That means you lose no quality after saving and editing the file, no matter how many times you do it. This makes TIFF files perfect for images that require big editing jobs in Photoshop or other photo editing software.


1 Answers

Change your last line to:

myBitmap.Save(new_fileName, myImageCodecInfo, myEncoderParameters);
like image 171
Nick Gotch Avatar answered Sep 19 '22 02:09

Nick Gotch