System.Drawing.Image.Save 抛出ExternalException:GDI 中发生一般错误
我有一个函数,它需要一个位图,复制它的一部分并将其保存为 8bpp tiff。结果图像的文件名是唯一的并且文件不存在,程序有权写入目标文件夹。
void CropImage(Bitmap map) {
Bitmap croped = new Bitmap(200, 50);
using (Graphics g = Graphics.FromImage(croped)) {
g.DrawImage(map, new Rectangle(0, 0, 200, 50), ...);
}
var encoderParams = new EncoderParameters(2);
encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 8L);
encoderParams.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionNone);
croped.Save(filename, tiffEncoder, encoderParams);
croped.Dispose();
}
奇怪的是,这个函数在某些计算机(Win 7)上运行良好,并抛出 System.Runtime.InteropServices.ExternalException:在其他计算机(主要是 Win XP)上的 GDI 异常中发生一般错误。
所有计算机都安装了 .NET 3.5 SP1 运行时。
如果我使用 croped.Save(filename, ImageFormat.Tiff);
而不是 croped.Save(filename, tiffEncoder,encoderParams);
它适用于所有计算机,但我需要以 8bpp 格式保存 Tiff。
您有什么想法吗?问题可能出在哪里?
谢谢,卢卡斯
I have function which takes a bitmap, copies part of it and saves it as 8bpp tiff. Filename of the result image is unique and file doesn't exist, program has permission to write to the target folder.
void CropImage(Bitmap map) {
Bitmap croped = new Bitmap(200, 50);
using (Graphics g = Graphics.FromImage(croped)) {
g.DrawImage(map, new Rectangle(0, 0, 200, 50), ...);
}
var encoderParams = new EncoderParameters(2);
encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 8L);
encoderParams.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionNone);
croped.Save(filename, tiffEncoder, encoderParams);
croped.Dispose();
}
Weird thing is that this funcion works well on some computers (Win 7) and throws System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI exception on other computers (mostly Win XP).
All computers have the .NET 3.5 SP1 runtime installed.
If I use croped.Save(filename, ImageFormat.Tiff);
instead croped.Save(filename, tiffEncoder, encoderParams);
than it works on all computers, but I need to save Tiff in 8bpp format.
Do you have any ideas, where the problem could be?
Thanks, Lukas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GDI是Windows操作系统的一项功能。我在处理 16 位 TIFF 文件时遇到了类似的问题,并求助于不同的库。请参阅 使用 c# 中的 LibTIFF
MSDN 帮助建议该功能可用,但当您尝试将位图复制到新位图或将其保存到文件或流时,Windows 会抛出“一般错误”异常。事实上,相同的功能在 Windows7 上运行良好(它似乎具有良好的 TIFF 支持)。请参阅Windows 7 中的新 WIC 功能。
我使用的另一个解决方案是制作 8 位的不安全副本。这样我就可以保存 PNG 文件(带调色板)。我还没有为 TIFF 尝试过这个。
GDI is a windows operating system functionality. I encountered similar issues when dealing with 16-bit TIFF files, and resorted to a different library. See using LibTIFF from c#
The MSDN help suggest that the functionality is available, but at then Windows throws 'generic error' exceptions when you try to copy a bitmap into a new bitmap, or save it to a file or stream. Indeed the same functionality works well on Windows7 (which seems to have good TIFF support). See New WIC functioanity in Windows 7.
The other solution I have used is to make a unsafe copy in 8-bit. This way I was able to save PNG files (with palette). I have not tried this for TIFF.