Dynamic PNG Image Generation with ASP.NET

I had created a small web app in MonoDevelop that created a png image on the fly. Porting the app to Windows .NET 3.5 with Visual Studio 2008 was almost painless, but it did include two unexpected incident. The first one was… well the app didn’t run anymore, showing the dreadful “A generic error occured in GDI+”. A bit of a research lead to this article here. Apparently the problem occurs only with PNG images. The stream that you write the generated image to needs to be seekable and I guess in Mono the Response.OutputStream is seekable but in .NET it’s not. The solution is to use an intermediate MemoryStream to write the image to and then dump that stream on Response.OutputStream.

After that was fixed, I noticed that the fonts were appearing ugly, as shown in the following graphic:

png-ugly-fonts

The solution to that was to fill the background of the entire image with a colour (white in my case). I think it must be related with PNG transparency. If you observe the above screenshot, the text appears ugly only in the area that is not filled with colour.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s