Displaying Dynamically Generated Images in ASP.NET

June 8, 2009 | By | 5 Comments

Sometimes you need to display an image that is not saved in a file. These could be dynamically generated or loaded from a file but were modified. To display them in an Image control you need to create another page that saves the image in its output stream.

As an example to demonstrate how to do this I created a project with two aspx pages. The first page, default.aspx, is the one the user views and it contains the Image control that will display the dynamically generated image. The second one contains the code to generate the image, named imagegen.aspx. Delete all the HTML code in imagegen.aspx and leave the Page directive only. So the contents of imagegen.aspx should look like this:

The code below is in imagegen.aspx’s Page Load event. It creates an image with a white background and writes the current date and time on it. The Bitmap object’s Save method allows you to save the image to a file or a stream. And saving the image to Response.OutputStream sends it to the outgoing HTTP body. To get the image displayed in default.aspx just set the ImageURL property of the Image control to “imagegen.aspx”. Now everytime default.aspx is loaded a new image is generated with the loading date and time displayed in it.

 

Listed at Hot Scripts in ASP.NET

 

Download Source Code

 

 

 

Filed in: ASP.NET | Tags: ,

Comments (5)

  1. Rukshan

    This was very helpful. Thank you very much for the article.

    Keep up the good work !!!

  2. Good one…But want to save the generated images in to a folder. How can we achieve that ?

    Thanks.

  3. @Ramu, you could call image.Save() again and pass the file name as string.

  4. i search this code senks

  5. Dorababu

    Hi this works fine but on the web page I am getting some characters after running the code

Leave a Reply

Trackback URL | RSS Feed for This Entry