Png images and ASP.NET… Getting a “A generic error occurred in GDI+”?

Another one I would have spent hours on was solved in two minutes by the wonder that is the internet. More specifically Chris Garrett over at ASPAlliance explains the solution to the png generic error in GDI+ exception .

The short story is you can’t use the Bitmap Save() method with a non-seekable stream. So, instead of just doing this:

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

You have to do something like this:

MemoryStream memoryStream = new MemoryStream();
img.Save(memoryStream, ImageFormat.Png);
memoryStream.WriteTo(context.Response.OutputStream);

Cheers Chris, you saved me hours of work!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s