whateverblog.
Graphics programming
Thursday, May 08, 2003 12:22 PM

I've always associated GDI programming with user interface widgets. It only very recently occurred to me that these same methods that are used to paint pixels on the screen, can also be used to manipulate image files. This fulfills a long-running dream of mine, to be able to manipulate .gif and .jpg files the way most programmers can manipulate .txt or database data.

Last weekend I was able to write a short C# program that recurses through a directory and resizes any .jpg's it finds there to fit within 600x600. Then I got a little more ambitious and added a routine to make thumbnails: resize to within 100x100, expand canvas to 120x120 with a white background, and overlay a specified .gif file.  The result:

(Notice that the thin gray border is part of the image... that was the whole point.)

All this with only a few commands--Windows' own graphics libraries do all the work. The same API provides the ability to draw text in any font/style you can imagine. So it'd be easy to create, for example, a dynamic headline .gif generator.

The only fly in the ointment is that .NET doesn't seem to do a very good job of reducing 24-bit images to 8-bit--it just uses a fixed 8-bit palette instead of choosing 256 colors from the image--and you pretty much have to work with a file in 24-bit to get anything done. So in practice, the useful (color) output formats are 24-bit PNG and JPEG. At least you can specify the compression ratio on the JPEG.

Anyway, none of this is news to experienced Windows programmers. But as someone who not too long ago was "just" a web developer, I'm pretty pumped.