Image manipulation in ASP.NET with system.drawing
page 1 of 1
Published: 23 Mar 2004
Unedited - Community Contributed
How would you like to ditch all those cumbersome components, command-line tools and workarounds and work with images in your ASP source? This article demonstrates creating an image in memory, and outputting it to the response stream so you can embed it into a page just as if it was an image file.
by Chris Garrett
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 53208/ 55


One criticism often levelled at ASP was that alternative platforms and languages are much more feature rich out of the box or better supported with free libraries.

Take PHP as an example, or have a look at CPAN, and you will see that there is a case to be made.

Now an ASP programmer could point to all the COM .dll's out there or point out that the Java people have an even harder time of it, "Try doing a simple string search and replace" you could say, but you do not need to fall back on such weak arguments any longer, now we have .NET :O)

One of the cool new features of the .NET framework is that one language has no great feature edge over another. Cobol can now use inheritance on C# classes, etc. On top of this, Microsoft have taken a lot of feedback from developers and have built-in some great new functionality. So what new goodies do we VB programmers get access to?

As a start, how would you like to ditch all those cumbersome components, command-line tools and workarounds and work with images in your ASP source?


The below example demonstrates creating an image in memory, and outputting it to the response stream so you can embed it into a page just as if it was an image file.

View the source here.

First we need to say that the output is not html, but an image MIME type. Then we import the namespaces of the system.drawing tools that we need. Next we clear the buffer, just to make sure there is nothing in there that could screw up our image format when it is output.

Next we are into the initialisation where we set up two objects, our image that will be output finally, and a working graphics object which will be our artistic toolbox.

The application variable is a really poor way of counting page accesses, but this example is about graphics, if there is enough interest I might follow up with a more robust and useful example!

To start working with our image we blank it using .clear. Imagine you are in photoshop or psp, the clear function not only "deletes" but you can set a colour to delete with, hence the parameter "color.yellow". We also set antialiasing on, at higher font sizes the jaggies are a pain, if this was a real counter you would use a small font size and antialiasing wouldnt be an issue.

Next we draw out our text string with an awful long line of syntax. We specify the text string, the font, the "brush" and the X/Y location to place it.

We then have an even more complicated line to draw a graduated tint. We start with the brush top left, bottom right, start colour, end colour, then the rectangle of the fill itself. Because we have used .fromARGB, we have a nice fading effect as ARGB stands for the alpha, red, green, and blue values.

Finally we output the image and clean up. Outputting the image involves "saving" it to the http stream of the browser using the new response.outputStream.

That's it

I hope you have found this little example interesting, it was the smallest useful example I could think of that would give you an indication of the power just yearning to break out from under .NETs hood ;O)

User Comments

Title: I want to capture an Image of .net control at runtime   
Name: Raxita Chudasama
Date: 2010-04-20 12:09:22 AM
I want to capture an image of .net controls like a panel,button etc.
I found a lot but i can't get a code in .net 3.5
Title: No found source code   
Date: 2009-10-06 7:07:17 AM
I have tried to download the source code in all the browsers, but not found that.

Please Check the issue
Title: editing existing image   
Name: Balaji
Date: 2008-05-29 3:55:27 AM
hi, am trying to edit the existing image. am able to draw lines but not in the exact position where am selected in window using mouse positions. Even if am saving the image the line will be displayed some where else. Even if i display the image on outputstream it is having the same problem
please do the needful
Title: Image Zooming   
Name: Fahim Anwar
Date: 2007-07-02 5:54:48 AM
hi , I am a software engineer and at the time me finding proper solution of zooming any image in or out. Also if image magnification faclity can be possible,

your help can be great

Fahim Anwar
Title: online broucher designing in   
Name: Touqeer Hameed
Date: 2006-01-28 1:59:19 PM
I am working in a software house. My project is to make online broucher cards, editing in image,font etc in plz help me as soon as possible i want to insert photoshop functionality in Plz tell me control.
Title: Not quite complete   
Name: swapna
Date: 2005-10-08 4:49:21 AM
Actually,I am trying to fix the image to a particular width & height.When the user clicks on the hyperlink the image has to be resized to its original size.Please can you help me on this coding.
Title: MR.   
Name: Anurag Sharma
Date: 2005-01-07 6:06:50 AM
We have a image in ractangle form. We write a text on that image using system.drawing namespace.

We are facing color resolution problem.

We are not getting clear image with text.

Please guide me, what can be the problem.
Title: You really neeed an aspx page to serve the picture?   
Name: Lars Netzel
Date: 2005-01-06 3:41:26 AM
Do you really really need an aspx page to serve as the image.. is there no way to make it all happen from a Sub or Function instead.. maybe create a "fake" aspx page in runtime that gives the Images? I mean... I want to render a bunch of HTML code from a WebControl LIbrary and I don't want to have to have an aditional aspx page for it as well...
Title: Other stuff in the page   
Name: GDI+ ASP.NET Articles
Date: 2004-09-15 2:45:09 AM
As you will see in the other GDI+ articles, you have to output the html and image seperately. You reference the dynamically generated image in your seperate HTML page using a IMG SRC that point to your ASPX page as if it was a static JPG or GIF. Hope this helps
Title: Not quite complete   
Name: Mark Jarzebowski
Date: 2004-09-15 1:23:14 AM
Terrific artcile.

But how do you place other stuff on the page? It appears that using ", imageformat.jpeg)" outputs only the image and nothing else can be showb on the page.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2017  |  Page Processed at 2017-03-26 9:04:15 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search