Troubleshooting a .NET error – A Generic Error in Occured in GDI+

Number of Views : 8556

I recently launched a new application that generates an image. In the app I created a custom image handler that returns a 2D barcode. The Handler used the System.Drawing class to generate the image. This all worked great locally on both Visual Studios Cassini and my local IIS box. However when I published the application to my server (win 2008) I was getting this obscure error “A generic error occurred in GDI+.”

What a great informative error. I did a quick google search only to find that this is a common issue. Many of the posts suggest this is a security issue, and that may be true is you were saving the image to a file structure. In my app, I am streaming the results out of memory back to the context in the handler, so it can display the image.

The offending line of code was:

context.Response.ContentType = "image/Jpeg";
System.Drawing.Image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)

I started looking at permissions, I assumed that since 90% of the fixes I read were done with permissions that this would be the simple answer. I was wrong, in my case permissions did not help. In a last ditch attempt I changed the image type. I was using System.Drawing.Image.ImageFormat.Jpeg I changed this to ImageFormat.Png.

This simple format change allowed the page to render on the server. I am not sure why the rendering of a jpg would be more of an issue than a PNG but I am ok with the result.

Fixed code
The offending line of code was:

context.Response.ContentType = "image/Png";
System.Drawing.Image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png)

If you are unfortunate enough to have this error I hope this post helps you. Good luck with the dreaded A generic error occurred in GDI+ error.

Leave a Reply

Your email address will not be published. Required fields are marked *