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

前端 未结 3 1329
情话喂你
情话喂你 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:20

    Never destroy the underlying streams from which your System.Drawing objects were created. Preserve all of your source streams, preventing them from going out of scope. This includes any byte arrays. IF you can't avoid losing scope of your original streams, consider using System.Array.Copy to make clean copies of the underlying byte arrays which can later be used to re-compose your streams and thus re-compose your images.

    Read this article: http://support.microsoft.com/kb/814675 (pay attention to the Symptom and Cause paragraphs. ignore the Workarounds paragraph it's useless.)

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-01-15 05:31

    Take a look at: https://support2.microsoft.com/Default.aspx?id=814675 If the stream was destroyed during the life of the Bitmap object, you cannot successfully access an image that was based on a stream.

    0 讨论(0)
提交回复
热议问题