.Net Image.Save将CTTIT传真4的tiff更改为LZW

时间:2010-12-23 04:55:50

标签: c# tiff

当我旋转图像时,.Net会切换tiff编码。有没有办法可以保留CCITT传真4(第4组传真编码),而不是让它切换到LZW?以下是我在磁盘上旋转图像的方法。

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);

谢谢, 布赖恩

更新:这是基于链接到下面的文章的代码。我想在这里添加完整的代码。另外,我设置了水平分辨率,因为位图默认为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;
}

1 个答案:

答案 0 :(得分:6)

Image类不会为您提供必要的细粒度控件。

为此,您需要读入位图,创建TIFF编码器,设置压缩类型的参数,然后让Bitmap对象使用该编解码器和参数保存图像。

这是一个应该引导你正确方向的例子:

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoder.compression.aspx

目前我的Mac上没有VS打开。

以下是更多详情:

http://social.msdn.microsoft.com/Forums/en/windowswic/thread/e05f4bc2-1f5c-4a10-bd73-86a676dec554