.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 (encoder开发者_如何学Pythons[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
The Image class isn't going to give you the necessary granular controls.
To do this, you'll need to read into a Bitmap, create a TIFF encoder, set the parameter for the type of compression, then have the Bitmap object save the image using that codec and parameter.
Here's an example that should lead you the right direction:
http://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoder.compression.aspx
I don't have VS open, on my Mac at the moment.
Here are more details:
http://social.msdn.microsoft.com/Forums/en/windowswic/thread/e05f4bc2-1f5c-4a10-bd73-86a676dec554
精彩评论