AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=400&pId=-1
Image manipulation in ASP.NET with system.drawing
page
by Chris Garrett
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 17916/ 13

System.Drawing

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?

System.Drawing

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)


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 9:34:23 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search