我有一组 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);
这整件事变得非常令人沮丧。请帮忙?!