且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

加载,保存,然后再次加载图像会引发“GDI +中发生一般错误”。

更新时间:2023-09-11 13:53:40

我最近认为是同样的问题。您需要跳过创建MemoryStream的using语句。创建位图会保留对创建它的流的引用。您可以在 MSDN 上阅读相关内容。

I had what I believe was the same problem recently. You need to skip the using statement around the creation of your MemoryStream. Creating a bitmap keeps a reference to the stream that created it. You can read about it on MSDN.

private static Bitmap LoadImageFromBytes(byte[] bytes)
{
    var imageStream = new MemoryStream(bytes))
    var image = (Bitmap)Bitmap.FromStream(imageStream);
    return image;
}