Click here to Skip to main content
12,951,034 members (58,229 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 9 Jul 2010

ASP.NET Generating Images on the Fly

, 9 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This article is a very quick description around how to generate images on the fly for an ASP.NET web application.


In Windows applications, the graphic’s library can help us generate images. Similar techniques can be applied to web applications. In this article, we explore these techniques.

At times, we need to create graphs and charts or some sort of image or may be add a water mark / copyright notice and so on.

Until HTML 5 is around and we have the canvas tag supported, this technique can help you to create images for a web application on the fly.


The audiences of this article are expected to know about .NET, Graphics library and the concept of HTTP handler.

The Basic Idea

The basic idea is very simple:

  1. Use the library to create any image. We may load an image from any stream (file, DB or any other place).
  2. Set the response type to an image of the required type (GIF, JPEG, BMP…).
  3. Response.Write the image binary stream hence pushing this image to the client.

The Code

Let's jump into the code as there is not much to talk about here.

The first thing we do is initialize the Graphics classes:

Bitmap bitMap = new Bitmap(300, 200); 
Graphics graphics = Graphics.FromImage(bitMap); 

Then we start to do some drawings inside it. For this example, we make the background golden and add a circle, a square, lines and curves.

SolidBrush solidWhiteBrush = new SolidBrush(Color.Gold); 
graphics.FillRectangle(solidWhiteBrush, 0, 0, 300, 200); 
graphics.FillEllipse(new LinearGradientBrush(new Rectangle(0, 100, 50, 50), 
	Color.Plum, Color.Red, 20), 0, 100, 50, 50); 
graphics.DrawLine(new Pen(Color.Black), 50, 125, 100, 125); 
graphics.FillRectangle(new SolidBrush(Color.Purple), 100, 100, 50, 50); 
graphics.DrawBezier(new Pen(Color.RoyalBlue), 100, 70, 200, 80, 70, 250, 200, 200); 

Finally, we write this image as a binary data to the stream. It should be noted that the content type is image/giv and the Save is being done onto a stream as a GIF.

Response.ContentType = "image/gif"; 
bitMap.Save(Response.OutputStream, ImageFormat.Gif); 

How Will I Make it Work in a Real Application?

In a real world application, this code should be written in an HTTP handler while the image is displayed in an img tag.


  • 10th July, 2010: Initial post


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


About the Author

Architect Imfinity
India India
Hi I have been working on enterprise applications for last six years.

You may also be interested in...

Comments and Discussions

GeneralSystem.Drawing Namespace Not Supported In ASP.NET 2.0 Pin
KevinAG27-Jul-10 12:10
memberKevinAG27-Jul-10 12:10 
GeneralNeat Technique Pin
DonDriskell12-Jul-10 8:26
memberDonDriskell12-Jul-10 8:26 
GeneralRe: Neat Technique Pin
gaurav_verma_mca12-Jul-10 17:54
membergaurav_verma_mca12-Jul-10 17:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 10 Jul 2010
Article Copyright 2010 by gaurav_verma_mca
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid