【发布时间】:2014-01-17 17:57:55
【问题描述】:
创建 tiff 文件,使用 LZW 压缩(默认):-
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 格式。谁能帮我解决这个问题?
更新:
尽管该方法现在可以在我使用 Visual Studio 2010 的 Windows 7 32 位操作系统上运行。一旦发布到 Windows 2008 服务器,该解决方案就不起作用(仅适用于 CCITT 压缩技术)。以下链接解释了原因。
根据本文,这是我正在尝试但无法将压缩应用于原始图像:-
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);
但是,我的最终结果是压缩正确的文件,但背景为纯黑色。似曾相识。
【问题讨论】:
-
我相信 CCITT 仅适用于双层图像,不是吗?所以像素要么是纯黑色的,要么是纯空的。有关更多信息,请参阅en.wikipedia.org/wiki/Group_4_compression。你的图片内容是什么样的?
-
我已经展示了我的图像内容的一些示例代码(在代码的创建部分)。基本上,用户在 Web 表单上的输入被翻译成文本(白色背景的黑色文本)到 tiff 文件中。
-
是的,CCITT 仅用于双层成像。我的文件中没有着色,也没有任何 3 维花哨的图形。只是 tiff 文件上的纯文本。
-
只是在这里扔东西来帮忙,但你的 tiff 不是多页或分层的,是吗?没有任何透明度?
-
事实上,看起来您并没有明确设置任何背景填充。你有多确定它会默认为白色而不是透明的?