Click here to Skip to main content
12,816,401 members (31,246 online)
Click here to Skip to main content
Add your own
alternative version


48 bookmarked
Posted 26 Oct 2007

Dynamic Images with ImageTemplate.NET

, 26 Oct 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
ImageTemplate.NET lets you easily generate images dynamically for use on your website.

An example of an animated gif generated by ImageTemplate.NET


Have you ever had the need to generate images on the fly in your ASP.NET applications? You could use GDI+ to do this directly, but we think we have an easier way. Enter ImageTemplate.NET. ImageTemplate.NET lets you configure an image template in an XML file and then change the image that is generated by passing parameters in the URL.

This is great for dynamically generating images based on database content. ImageTemplate.NET can help you:

  • Rotate images
  • Generate animated GIFs dynamically
  • Generate images from strings so you can use any font you like on your website
  • Resize images to create thumbnails

ImageTemplate.NET caches generated images so it performs almost as well as static images.


Basic knowledge of ASP.NET is required to get started with ImageTemplate.NET. The animated GIF generation is done using the component Gif.Components.dll, and some code from Paint.Net is used to help do high quality rotation of images.

Using the code

Simply download the source code to get started with the sample web app.

To host ImageTemplate.NET in your own web application, add these settings to your web.config:

<?xml version="1.0"?>
        <section name="imageTemplate" 
  config = The path to the file where the templates are defined 
  imageCacheDir = The location that images should be cached after being generated. 
                  This can be anywhere the web server has write access to
  defaultCacheTimeSeconds = The amount of time that images should be kept in the cache
    <imageTemplate config="~/templates/ImageTemplates.config" 
            <!-- If the app is installed as a virtual directory of another 
               ASP.NET website we need to remove the modules so we don't get 
               issues with dlls not being found -->
            <add verb="GET,HEAD" path="TemplateImageGenerator.axd" 
                      ImageTemplateNet" validate="false"/>

The work of configuring your templates is done in ImageTemplates.config. This is also used to setup the element types which are the bits of code that know how to actually draw the template.

<?xml version="1.0"?>
  You define your image templates in this file. 
  You may remove any of the <template> elements
  in here you don't want
  <!-- The definition of the element types that you can use when building image templates.
  an elementType specifies the bit of code that does the drawing for a template -->
  <elementType id="Panel" 

  <elementType id="Image" 

  <elementType id="Text" 

  Define your imate templates here
  <!-- An example of defining a template in another file -->
  <template id="productBanner" 

 <!-- An example of defining a template in line -->
  <template id="fontDemo" 
     imageFormat="Jpeg" backgroundColor="#FFFFFF">
  <element id="textArea" x="0" 
     y="0" height="200" type="Text">
  <FontFamily>Franklin Gothic Demi</FontFamily>
  <Text>Canon IXYuuuuu</Text>

To generate an image using the "fontDemo" template, you would use a URL like: /TestWebsite/TemplateImageGenerator.axd?template=fontDemo&textArea.Text=Hello+World.

You could generate an image using the template as a base and then set a different font size using: /TestWebsite/TemplateImageGenerator.axd?template=fontDemo&textArea.Text=Canon+IXYuuuuu&textArea.FontSize=50.

More examples can be found by downloading the code for the article, or at ImageTemplate.Net.

The next article in this series will explain how to write your own image template element types.


  • 1.1: First public release.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Michael is the founder and managing director of Flipmind Ltd a IT consultancy based in Auckland, New Zealand.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 27 Oct 2007
Article Copyright 2007 by flipmind
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid