.Net Image.Save changes tiff from CTTIT Fax 4 to LZW

When I rotate an image, .Net switches the tiff encoding. Is there a way I can keep the CCITT Fax 4 (Group 4 Fax encoding) and not have it switch to LZW? Here is how I am rotating an image on disk.

System.Drawing.Image img = System.Drawing.Image.FromFile(input);
//rotate the picture by 90 degrees
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
img.Save(input, System.Drawing.Imaging.ImageFormat.Tiff);

Thanks, Brian

Update: Here's the code based on the articles linked to below. I wanted to add the code here for completenes. Also, I set the horizontal resolution because the bitmaps defaults to 96 DPI.

//create an object that we can use to examine an image file
System.Drawing.Image img = System.Drawing.Image.FromFile(input);

//rotate the picture by 90 degrees
img.RotateFlip(RotateFlipType.Rotate90FlipNone);

// load into a bitmap to save with proper compression
Bitmap myBitmap = new Bitmap(img);
myBitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);

// get the tiff codec info
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/tiff");

// Create an Encoder object based on the GUID for the Compression parameter category
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Compression;

// create encode parameters
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;

// save as a tiff
myBitmap.Save(input, myImageCodecInfo, myEncoderParameters);

// get encoder info for specified mime type
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;
}
6
задан BrianK 23 December 2010 в 17:47
поделиться