本文介绍了应用CCITT COM pression我的TIFF文件后,它产生的图像具有坚实的黑色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建TIFF文件,与LZW COM pression(默认): -

Creating tiff file, with LZW compression (default):-

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

所以,我想它的COM pression改为CCITT: -

So I am trying to change its compression to 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文件的COM pression类型是否正确被修改为CCITT(通过右击输出文件和检查属性)。
然而,当我尝试打开它,在黑色的..没有了整幅图像就可以了。

The process works and I can see that the compression type of my tiff file has correctly been modified to CCITT (by right clicking on the output file and checking properties).However when I try to open it, the whole image in black.. nothing is on it.

当我更改COM pression类型LZW或无或RLE那么TIFF文件又是一次的可视图像。

When I change the compression type to LZW or None or RLE then the tiff file is once again a viewable image.

不幸的是我只需要CCITT格式。谁能帮我算出这个?

Unfortunately I need the CCITT format only. Can anyone help me figure this out?

更新:

尽管方法现在在我的Windows作品使用Visual Studio 2010。一旦发布到Windows 2008的服务器7 32位操作系统,该解决方案不起作用(仅适用于CCITT COM pression技术)。下面的链接解释了原因。

Even though the method now works on my windows 7 32 bit OS using Visual studio 2010.. Once published to the Windows 2008 server, the solution doesn't work (only for CCITT compression techniques). The following link explains why.

<一个href=\"http://social.msdn.microsoft.com/Forums/vstudio/en-US/1585c562-f7a9-4cfd-9674-6855ffaa8653/parameter-is-not-valid-for-com$p$pssionccitt4-on-windows-server-2003-and-2008?forum=netfxbcl\" rel=\"nofollow\">http://social.msdn.microsoft.com/Forums/vstudio/en-US/1585c562-f7a9-4cfd-9674-6855ffaa8653/parameter-is-not-valid-for-com$p$pssionccitt4-on-windows-server-2003-and-2008?forum=netfxbcl

根据这篇文章,这就是我想,但不能得到COM pression应用到原始图像: -

As per this article, this is what I am trying but can't get the compression to apply to the original image:-

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

不过,我最终的结果是正确的COM pression一个文件,而是一个坚实的黑色背景。似曾相识。

However, my end result is a file with the correct compression, but a solid black background. Deja Vu.

推荐答案

编辑code这样,你就可以使用所需的COM pression没有黑色背景填充:

Edit your code as such, and you will be able to use your desired compression without the black background fill:

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

这篇关于应用CCITT COM pression我的TIFF文件后,它产生的图像具有坚实的黑色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 21:39