GDI+ Generic Error

前端 未结 1 1505
悲&欢浪女
悲&欢浪女 2021-01-20 05:19

When my images are being loaded from my database on my web server, I see the following error:

A generic error occurred in GDI+. at System.Drawing

相关标签:
1条回答
  • 2021-01-20 05:44

    Probably for the same reason that this guy was having problems - because the for a lifetime of an Image constructed from a Stream, the stream must not be destroyed.

    So if your GetImage function constructs the returned image from a stream (e.g. a MemoryStream) and then closes the stream before returning the image then the above will fail. My guess is that your GetImage looks a tad like this:

    Image GetImage(int id)
    {
        byte[] data = // Get data from database
        using (MemoryStream stream = new MemoryStream(data))
        {
            return Image.FromStream(data);
        }
    }
    

    If this is the case then try having GetImage return the MemoryStream (or possibly the byte array) directrly so that you can create the Image instance in your ProcessRequest method and dispose of the stream only when the processing of that image has completed.

    This is mentioned in the documentation but its kind of in the small print.

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