ASP.NET Image Generation

ASP.NET Image Generation is a speedy way of generating, transforming, caching images in
Download dll and Quickstarts here: ASP.NET Generated Image
Simple Generated Image, Database Images, Custom Image Transforms

Step 1: Add ImageGeneration.dll to Bin.


Step 2: Add Generic Handler.

File -> New Item -> Generic Handler

Step 3: Code away.

<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Web;
using Microsoft.Web;
using System.Drawing;

public class MyImageHandler : ImageHandler {
    public MyImageHandler()
        this.ServerCache = true;
        this.ClientCache = true;
        this.ImageTransforms.Add(new ImageResizeTransform { Width = 100, Height = 100 });

    public override ImageInfo GenerateImage(System.Collections.Specialized.NameValueCollection parameters)
        Bitmap bit = new Bitmap(300, 60);
        Graphics gra = Graphics.FromImage(bit);
        gra.DrawRectangle(Pens.Black, 0, 0, 300, 60);        
        gra.DrawString(parameters["text"], new Font(FontFamily.GenericSansSerif, 16), Brushes.Black, 0, 0);

        return new ImageInfo(bit);

Goto: <a href="http://<your web app>/MyImageHandler.ashx?text=hello from your friendly image handler">http://<your web app>/MyImageHandler.ashx?text=hello from your friendly image handler

GeneratedImage Control

<cc1:GeneratedImage ID="GeneratedImage1" runat="server" ImageHandlerUrl="~/MyImageHandler.ashx" >            
          <cc1:ImageParameter Name="text" Value="This is realy cool" />

Last edited Oct 27, 2008 at 1:58 PM by ScottGal, version 2


MitchDenny Nov 2, 2008 at 8:55 PM 
One of the key challenges with this kind of dynamic image generation isn't doing it with a handler - this handler base class saves us about three lines of code. It would be better if we could have some kind of image generator where I could register a bitmap as a web-resource to be sent back to the browser. The challenges will be to make it stateless so it can be used across web-farms. Maybe if we could register a callback or something that was then serialized into a URL - so that on the subsequent response any server could generate the image.

Either that - or make ASP.NET more aware of the other nodes in the farm - but that would be a complex architectural change.

n1ck0s Oct 29, 2008 at 5:15 PM 
I'm not sure where to download "ImageGeneration.dll" from. There is an ImageHandler in "Microsoft.Web.GeneratedImage.dll" but I am under the impression that "ImageGeneration.dll" is an earlier revision of that code. Can anyone help?