在我的Tiff文件上应用CCITT压缩后,它会生成一个带有纯黑色背景的图像

时间:2014-01-17 17:57:55

标签: c# asp.net tiff

使用LZW压缩创建tiff文件(默认): -

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(800, 1000); 
Graphics g = Graphics.FromImage(bitmap);
g.DrawString("Name: " + Name.Text, outputFont, Brushes.Black, new PointF(0, 20));
g.DrawString("Date Of Birth: " + Date_Of_Birth.Text, outputFont, Brushes.Black, new PointF(0, 40));
g.DrawString("Address: " + Address.Text, outputFont, Brushes.Black, new PointF(0, 60));
g.DrawString("City: " + City.Text, outputFont, Brushes.Black, new PointF(0, 80));
g.DrawString("State: " + State.Text, outputFont, Brushes.Black, new PointF(0, 100));
g.DrawString("Zip Code: " + Zip_Code.Text, outputFont, Brushes.Black, new PointF(0, 120));
g.DrawString("Phone: " + Phone.Text, outputFont, Brushes.Black, new PointF(0, 140));
g.DrawString(" ID: " + ID.Text, outputFont, Brushes.Black, new PointF(0, 160));
fileName = saveDirectory + id + ".tif";
bitmap.Save(fileName, ImageFormat.Tiff);

所以我试图将其压缩改为CCITT: -

Bitmap myBitmap;
myBitmap = new Bitmap(fileName);
ImageCodecInfo myImageCodecInfo;
myImageCodecInfo = GetEncoderInfo("image/tiff");
System.Drawing.Imaging.Encoder myEncoder;
myEncoder = System.Drawing.Imaging.Encoder.Compression;
EncoderParameters myEncoderParameters;
myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter;      
myEncoderParameter = new EncoderParameter(myEncoder,(long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save(new_fileName, myImageCodecInfo, myEncoderParameters);

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

该过程有效,我可以看到我的tiff文件的压缩类型已正确修改为CCITT(通过右键单击输出文件并检查属性)。 然而,当我尝试打开它时,整个图像都是黑色的......没有任何内容。

当我将压缩类型更改为LZW或None或RLE时,tiff文件再次成为可视图像。

不幸的是我只需要CCITT格式。任何人都可以帮我解决这个问题吗?

更新

即使该方法现在可以在我的Windows 7 32位操作系统上使用Visual Studio 2010工作。一旦发布到Windows 2008服务器,该解决方案不起作用(仅适用于CCITT压缩技术)。以下链接解释了原因。

http://social.msdn.microsoft.com/Forums/vstudio/en-US/1585c562-f7a9-4cfd-9674-6855ffaa8653/parameter-is-not-valid-for-compressionccitt4-on-windows-server-2003-and-2008?forum=netfxbcl

根据这篇文章,这是我正在尝试但无法将压缩应用于原始图像: -

        int width = 800;
        int height = 1000;
        int stride = width/8;
        byte[] pixels = new byte[height*stride];

        // Try creating a new image with a custom palette.
        List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
        colors.Add(System.Windows.Media.Colors.Red);
        colors.Add(System.Windows.Media.Colors.Blue);
        colors.Add(System.Windows.Media.Colors.Green);
        BitmapPalette myPalette = new BitmapPalette(colors);

        // Creates a new empty image with the pre-defined palette

        BitmapSource image = BitmapSource.Create(
            width,
            height,
            96,
            96,
            System.Windows.Media.PixelFormats.BlackWhite,
            myPalette, 
            pixels, 
            stride);


FileStream stream = new FileStream(Original_File, FileMode.Create);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Compression = TiffCompressOption.Ccitt4;
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);

但是,我的最终结果是压缩正确的文件,但背景为黑色。 Deja Vu。

1 个答案:

答案 0 :(得分:3)

如此编辑您的代码,您将能够在没有黑色背景填充的情况下使用所需的压缩:

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(800, 1000); 
Graphics g = Graphics.FromImage(bitmap);
/* Add this */ g.FillRectangle(Brushes.White, new Rectangle(0,0, 800, 1000));
g.DrawString("Name: " + Name.Text, outputFont, Brushes.Black, new PointF(0, 20));
g.DrawString("Date Of Birth: " + Date_Of_Birth.Text, outputFont, Brushes.Black, new PointF(0, 40));
g.DrawString("Address: " + Address.Text, outputFont, Brushes.Black, new PointF(0, 60));
g.DrawString("City: " + City.Text, outputFont, Brushes.Black, new PointF(0, 80));
g.DrawString("State: " + State.Text, outputFont, Brushes.Black, new PointF(0, 100));
g.DrawString("Zip Code: " + Zip_Code.Text, outputFont, Brushes.Black, new PointF(0, 120));
g.DrawString("Phone: " + Phone.Text, outputFont, Brushes.Black, new PointF(0, 140));
g.DrawString(" ID: " + ID.Text, outputFont, Brushes.Black, new PointF(0, 160));
fileName = saveDirectory + id + ".tif";
bitmap.Save(fileName, ImageFormat.Tiff);