Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDI+ Generic Error

Tags:

c#-4.0

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.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(Stream stream, ImageFormat format) at MyWeb.Helpers.ImageHandler.ProcessRequest(HttpContext context)

All my code is attempting to do is load the image, can anybody take a look and let me know what I'm doing wrong?

Note - This works if I test it on my local machine, but not when I deploy it to my web server.

public void ProcessRequest(HttpContext context)
{
    context.Response.Clear();          

    if (!String.IsNullOrEmpty(context.Request.QueryString["imageid"]))         
    {
        int imageID = Convert.ToInt32(context.Request.QueryString["imageid"]);
        int isThumbnail = Convert.ToInt32(context.Request.QueryString["thumbnail"]);

        // Retrieve this image from the database
        Image image = GetImage(imageID);

        // Make it a thumbmail if requested
        if (isThumbnail == 1)
        {
            Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            image = image.GetThumbnailImage(200, 200, myCallback, IntPtr.Zero);
        }

        context.Response.ContentType = "image/png";             

        // Save the image to the OutputStream             
        image.Save(context.Response.OutputStream, ImageFormat.Png);
    }         
    else         
    {             
        context.Response.ContentType = "text/html";             
        context.Response.Write("<p>Error: Image ID is not valid - image may have been deleted from the database.</p>");         
    } 
}

The error occurs on the line:

image.Save(context.Response.OutputStream, ImageFormat.Png);

UPDATE

I've changed my code to this, bit the issue still happens:

var db = new MyWebEntities();

var screenshotData = (from screenshots in db.screenshots
                      where screenshots.id == imageID
                      select new ImageModel
                      {
                           ID = screenshots.id,
                           Language = screenshots.language,
                           ScreenshotByte = screenshots.screen_shot,
                           ProjectID = screenshots.projects_ID
                      });

foreach (ImageModel info in screenshotData) 
 {
    using (MemoryStream ms = new MemoryStream(info.ScreenshotByte))
    {
         Image image = Image.FromStream(ms);

         // Make it a thumbmail if requested
         if (isThumbnail == 1)
         {
              Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
              image = image.GetThumbnailImage(200, 200, myCallback, IntPtr.Zero);
         }

         context.Response.ContentType = "image/png";

         // Save the image to the OutputStream             
         image.Save(context.Response.OutputStream, ImageFormat.Png);

    } }

Thanks.

like image 568
Jimmy Collins Avatar asked Apr 09 '26 09:04

Jimmy Collins


1 Answers

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.

like image 101
Justin Avatar answered Apr 11 '26 00:04

Justin