January 2004 - Posts

Dynamic image generation in ASP.NET 2.0

Not much has been said about this so far, although Dino Esposito has a couple of interesting posts here: Dynamic Images and here: Quality Of Dynamic Images .

When the .NET Framework first appeared, in addition to all the other benefits one of the great bonuses for web developers was the ability to generate graphics dynamically via the extensive support for GDI+ in the framework. The upcoming Whidbey version promises to make things even better, with the addition of the DynamicImage control (which as well as hosting dynamically generated images, gracefully handles image type conversion for various devices) and the ImageGenerator class. The latter is the base class for the image generation service that you can implement in a .asix file (currently there is as far as I can tell no support in the IDE for the .asix file type - I very much hope that changes by RTM...while they're about it they could add some for .ashx).

As I said above, not much has been said about these features (and .asix files in particular), although there is a bit in “A First Look at ASP.NET v. 2.0” (by Alex Homer, Dave Sussman and Rob Howard). ...my introduction to .asix started with Listing 12.4 (page 396 of the paperback edition). This illustrates how you can generate an image simply by overriding the RenderImage method inherited from ImageGenerator. I should however point out that Mr Proofreader could profitably have spent some quality time with this one: with the PDC build at least it should be inheriting from System.Web.UI.Imaging.ImageGenerator, not System.Web.UI.Image.ImageGenerator, and “Ariel” is one of the moons of Uranus rather than the name of a font. But I quibble ('cos it's fun).

My first experiments generating jpegs proved somewhat disappointing (poor quality) once I moved beyond balc and white, and I initially assumed that this was due to a limitation of the DynamicImage control. I soon realised the problem lay at source (in this case the image generator file).To have some control over the quality of a generated jpeg it appears you have to override the CreateImage method (because it, er, creates the image)...so many happy hours were spent tweaking EncoderParameters and setting antialising and so on...I found that to avoid serious colour bleeding and profound blurriness I had to set the quality to 100%, which seems a bit extreme, and it was still not that great. This is not however a bug with Whidbey, but is consistent with the quality of jpeg generation in current versions of the Framework. The odd (to me in my ignorance) thing is that if you are manipulating the quality and so on of an existing image, the results are much more what you would expect.  Just one of those things. PNG generation is a snap, so that would be a good substitute in a lot of cases where JPEG might otherwise come to mind. And for images without subtle colour gradations and with a fairly small number of colours (as in the safety palette) GIF is probably the best choice anyway. 

I haven't mentioned any of the many cool features of the DynamicImage control (or any of them really), because I thought at this stage I'd just highlight a few minor gotchas and make things easier for anyone who may happen to read this.

And for any Microsofties who may read this: Remember the IDE support for .ASIX! Pretty please.

First Post

Well, this is a hello to anybody who may be reading this and a big thank you to Donny and Co. for setting me up.

I had intended to have something useful and intelligent for my inaugural post, but my ISP has been having major problems tonight and I've had no access until now...and now I'm just tooooooooo sleepy (it's 1:23 am here in New Zealand).

So anyway, my .NET interests are especially and in no particular order General .NET Stuff, the Compact Framework (and device development in general) and Whidbey. Plus anything else that may enter my head.

I'm familiarising myself with the Whidbey and Yukon bits I brought back from the PDC so I'm likely to have quite a lot to say about that - unfortunately I wasn't able to install Longhorn because it appears not to like my motherboard :-(....which is a shame because apart from anything else I'm dying to try out Indigo - My initial reaction to it was similar to my initial reaction to ADO.NET coming from ADO: “What inspired them to make something simple so complicated?“...but as with ADO.NET, the more I've learned about it the more that reaction has changed to “Cooooooooooool!!!“ (and that's more exclamation marks than you'll ever see me use in one place again - I've always felt that more than one exclamation mark in a document is symptomatic of  either spam, insanity, or being a 14-year old girl (with apologies to any of the latter who may consider themselves impugned by that remark)).

Right, that's enough rambling from me, I'm off to bed.