“GDI 中发生一般错误” 将 Bitmap 对象保存为 jpeg 时,c#
我有一个 ushort 像素数据数组(16 位灰度值),我试图将其保存为 jpeg 图像。 但是,我的代码在保存命令时崩溃,并显示“GDI+ 中发生一般错误”。 我不知道如何解决这个问题。 我要保存的目录是由我的应用程序创建的,我向其中写入其他文件; 所以我知道这不是权限问题。 这可能是数据损坏问题吗? 我在将 ushort 数据放入 Bitmap 对象的步骤中做错了什么吗? 因为我有 ushort 数据,所以我发现需要花费一些精力才能弄清楚如何将其放入 Bitmap 对象中,而且我可能做错了。
这是我的代码:
Bitmap img = new Bitmap(width, height, PixelFormat.Format16bppGrayScale);
Rectangle rect = new Rectangle(0,0, width, height);
BitmapData picData = img.LockBits(rect, ImageLockMode.ReadWrite, img.PixelFormat);
IntPtr pixelStartAddress = picData.Scan0;
WriteableBitmap pic = new WriteableBitmap(width, height, 96.0, 96.0, System.Windows.Media.PixelFormats.Gray16, null);
int stride = (thumb.XSize * pic.Format.BitsPerPixel + 7) / 8;
pic.WritePixels(new System.Windows.Int32Rect(0, 0, width, height), dataArray, stride, 0);
pic.CopyPixels(new System.Windows.Int32Rect(0,0,thumb.XSize, thumb.YSize),pixelStartAddress, dataArray.Length * sizeof(ushort), stride);
img.UnlockBits(picData);
img.Save(path, ImageFormat.Jpeg);
这整件事变得非常令人沮丧。 请帮忙?!
I have an array of ushort pixel data (16-bit grayscale values), and I am trying to save it as a jpeg image. However my code is crashing at the Save command with "A generic error occurred in GDI+". I can't figure out how to fix this. The directory I am saving to is created by my application and I write other files to it; so I know it's not a permissions problem. Is it maybe a data corruption problem? Am I doing something wrong in the steps to get the ushort data into the Bitmap object? Because I have ushort data I found it took some effort to figure out how to get it into the Bitmap object, and I am possibly doing it wrong.
Here is my code:
Bitmap img = new Bitmap(width, height, PixelFormat.Format16bppGrayScale);
Rectangle rect = new Rectangle(0,0, width, height);
BitmapData picData = img.LockBits(rect, ImageLockMode.ReadWrite, img.PixelFormat);
IntPtr pixelStartAddress = picData.Scan0;
WriteableBitmap pic = new WriteableBitmap(width, height, 96.0, 96.0, System.Windows.Media.PixelFormats.Gray16, null);
int stride = (thumb.XSize * pic.Format.BitsPerPixel + 7) / 8;
pic.WritePixels(new System.Windows.Int32Rect(0, 0, width, height), dataArray, stride, 0);
pic.CopyPixels(new System.Windows.Int32Rect(0,0,thumb.XSize, thumb.YSize),pixelStartAddress, dataArray.Length * sizeof(ushort), stride);
img.UnlockBits(picData);
img.Save(path, ImageFormat.Jpeg);
This whole thing has become very frustrating. Please help?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我担心这与灰度/JPG 有关。 我不知道JPEG是否支持灰度。
我会尝试将位图声明为普通颜色位图。 和测试。
I'm, afraid it has something to do with the grayscale / JPG. I don't know if JPEG supports grayscales.
I would try declaring the bitmap as a normal color one. And testing.
我发现我绘制的任何位图,无论采用哪种方式,在保存为 JPEG 时都会偶尔出现问题(您看到的例外)。 有帮助的是首先克隆图像:
也许通过这样做,您还可以尝试更改像素格式,因为我也假设,就像 tekBlues 一样,可能存在灰度问题。
I have found that any bitmap that I had drawn on, no matter which way, had an occasional problem with being saved as JPEG (with the exception you are seeing). What helped was to clone the image first:
Maybe by doing this you can also try to change the pixel format, because I also assume, just like tekBlues, that there may be issues with greyscale.
关于您对图像质量的评论(我会作为评论进行回复,但它会丢失代码格式):
您正在使用 JPEG 导出的默认设置进行保存。 您可以使用更高的质量设置创建自己的 EncoderParameters 实例:
Regarding your comment about image quality (I would have responded as a comment, but it loses the code formatting):
You are saving with the default settings for JPEG export. You can create your own EncoderParameters instance with a higher quality setting:
遗憾的是,GDI+ 无法正确支持
PixelFormat.Format16bppGrayScale
。 我一直在研究这个长期存在的问题,但尚未找到解决方案。看来现代的方法是在 System.Windows.Media 中使用较新的 WPF API。
Unfortunately, GDI+ doesn't support
PixelFormat.Format16bppGrayScale
properly. I've been researching this long-standing issue and haven't found a solution.It seems that the modern day approach is to use the newer WPF APIs in
System.Windows.Media
.我敢打赌这是权限问题。 GDI+ 是本机组件,我相信它需要与常规文件保存或类似的权限不同的权限。 这是 ASP.NET 应用程序吗? 如果是这样,请确保您向 IIS 进程授予该文件夹的权限。
I am willing to bet that this is permissions problem. GDI+ is native component and I believe it requires different permissions than regular file saving or something like this. Is this ASP.NET app? If so make sure you give permissions to the IIS process for that folder.
当我保存的维度无效时,我已经看到过这种情况发生。 比如宽度为0。
I've seen this happen when the dimensions I was saving to weren't valid. Like a width of 0.