Friday, December 7, 2012

Outputing a PNG directly to an ASP.NET Response.OutputStream

So you need to generate an image dynamically. Perhaps you want to watermark it. Or change its size based on some information that isn’t available until runtime.

But the call to Image.Save(Response.OutputStream, …) fails with the following error:

A generic error occurred in GDI+.

Turns out PNGs require a stream that’s bidirectional. Response.OutputStream is unidirectional. The solution is to save the PNG to a MemoryStream then write the MemoryStream to the Response OutputStream.

Thanks to this article for clearing it up!

No comments :

Post a Comment