【问题标题】:.Net Image.Save changes tiff from CTTIT Fax 4 to LZW.Net Image.Save 将 tiff 从 CTTIT Fax 4 更改为 LZW
【发布时间】:2010-12-23 04:55:50
【问题描述】:

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

【问题讨论】:

    标签: c# tiff


    【解决方案1】:

    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

    【讨论】:

    • 谢谢你的作品。它将 DPI 从 300 更改为 96。有人知道怎么设置吗?
    • 我认为图像类确实支持这一点(至少它对我有用)。所以你不需要额外的位图,而且作为一个额外的好处,你不需要弄乱分辨率。
    • BrianK:Bitmap.SetResolution () 会做到的,(虽然这可能在 2 年后对你没有帮助!)
    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2013-02-27
    • 2015-01-20
    • 2011-05-05
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    相关资源
    最近更新 更多