Tuesday, August 30, 2005

Dynamically Generating Images in ASP .NET

In .NET it is very easy to output a raw image from a web page instead of the default HTML. All you have to do is change the content type and stream the image as binary data to the response:

    protected void Page_Load(object sender, EventArgs e)
        Bitmap bitmap = new Bitmap(320, 240);

        MemoryStream memoryStream = new MemoryStream();
        bitmap.Save(memoryStream, ImageFormat.Png);

        Response.ContentType = "image/png";

The above code will stream an empty image in the PNG format to the caller of this page. The image can either be rendered dynamically or read from a database, the sky is the limit. By using request parameters the image rendering can easily be customized.

The resulting image page can be embedded on other pages on the web site, either statically by referencing it in a HTML image element or dynamically by using the ASP .NET image server control. In the latter case you just assign the URL of the image page to the ImageUrl property of the control.

Het wordt wel eens tijd om de boel te updaten hoor!

-= Maarten =-
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?