c# - .Net Image.Save 将 tiff 从 CTTIT Fax 4 更改为 LZW

标签 c# tiff

当我旋转图像时,.Net 会切换 tiff 编码。有没有办法可以保留 CCITT Fax 4(Group 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;
}

最佳答案

Image 类不会为您提供必要的精细控制。

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

这是一个可以引导您正确方向的示例:

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

关于c# - .Net Image.Save 将 tiff 从 CTTIT Fax 4 更改为 LZW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4515964/

相关文章:

c# - 适用于 Windows Phone 8 的 Windows Azure 通知中心无法正常工作

c# - 启动后启用/禁用身份验证选项

java - 如何使用 Azure 在 Java 或 Android 中使用 Microsoft Translator API

java - 如何更改 ImageIO 写入字节顺序

python - 找不到 PIL group4 解码器

c# - 何时使用每种类型的循环?

c# - 我需要一个C#的分析工具

c - 编写未压缩的 JPEG 或 PNG 图像的简单方法?

ocr - 用于扫描文档(TIFF 和 PDF)的扫描仪的最佳设置

c++ - 如何读取自定义 TIFF 标签(无 TIFFFieldInfo)