Loading, saving, and then loading an image again throws “A generic error occurred in GDI+”

前端 未结 3 1328
情话喂你
情话喂你 2021-01-15 04:37

This seems to be in infamous error. I remember getting it a while back for different code, but it\'s back, with a vengeance, but with some new code that I can\'t se

3条回答
  •  孤街浪徒
    2021-01-15 05:21

    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;
    }
    

提交回复
热议问题