c# - 将 Bitmap 对象保存为 jpeg 时的 "A generic error occurred in GDI+",c#

标签 c# .net bitmap jpeg

我有一个 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);

这整件事变得非常令人沮丧。请帮忙?!

最佳答案

我担心这与灰度/JPG 有关。我不知道JPEG是否支持灰度。

我会尝试将位图声明为普通颜色位图。并进行测试。

关于c# - 将 Bitmap 对象保存为 jpeg 时的 "A generic error occurred in GDI+",c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/997902/

相关文章:

c# - 我可以在 C# 中声明命名的异步元组函数吗?

c# - 如何在 XAML 中换行或换行

.net - 为什么指针是引用类型?

android - 如何在android中使用API​​将base64编码的图像保存到MYSQL数据库中

安卓 : Bitmap causing out of memory error

c# - Oledb 和 DATETIME 的问题

c# - 如何以编程方式更改文件位置?

c# - DependencyPropertyChanged 回调事件未被触发

.net - C++ 到 .Net

C# 画图程序闪烁