ASP.NET June 09, 2010

My quest for a way to generate images with text using custom fonts with ASP.NET

I was asked today to look into creating an ASP.NET HTTP handler that on the fly creates images with text in a specific font. The handler had to meet a few requirements:

  • It had to be able to create images with transparent backgrounds.
  • It had to support both TrueType and OpenType fonts.
  • It had to support word wrapping when the text didn’t fit in a specified width.

Eager not to reinvent the wheel I set out to find a suitable open source component that met these demands. To my surprise that turned out to be easier said than done.

The quest for a image generating handler

It might just be me being a really bad Google user (I did ace the final exam in a weird course called “Information retrieval on the Internet” in high school though!) but all I could find was a few blog posts about creating such handlers with some source code attached. Finally I decided to give dotIR by Milan Negovan a try despite the fact that the latest version was a beta released in 2004.

I was pleasantly surprised. After having upgraded the project from a VS 2003 project to a VS 2010 project and converted it from a web project to a class library with a handler that I could map in web.config it worked like a charm. It did need a few adjustments though. I changed it so that it generated PNG:s instead of GIF:s and supported transparency. Also, it doesn’t support caching the images which is something I will definitely add my self. Just got to decide if I want to cache them in memory or on disk first.

Unable to find that OpenType font

Happy that I’d found what I thought was a fairly simple solution to my task I tried it using an OpenType font and was a bit surprised to find that it didn’t work. The handler was unable to find the font even though it was installed on the server. Turns out the font I was trying to use was an OpenType font with an internal PostScript font (as opposed to an OpenType font with an internal TrueType format) and GDI+ only support OpenType fonts with internal TrueType format.

While there are workarounds for this we decided to use a program called FontXChange to convert the OpenType font that we wanted to use to TrueType instead of spending more time on implementing the workarounds in the handler. I might do that some other day though :)

Know of a better solution?

I was a bit surprised to find that I couldn’t locate a more complete solution for generating images with text in them. If you know of another, better solution than the one that I found please let me know with a comment!

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more


comments powered by Disqus

More about ASP.NET