Alex Mueller on Software and Technology 
Wednesday, January 12, 2005
Manipulating images in .NET involves using the System.Drawing namespace, namely the Bitmap, its parent class Image, and Graphics classes. What I want to show with this post is that it is possible to transform an image that is already saved on the file system, or one that is referenced by a URL. So if your app has its own images, or you want to link to a served image on a different site, we can still manipulate the images and display them using the output stream. It's actually quite simple, but perhaps often overlooked.

My setup for this test involves creating an image using either web or html controls, and setting the source of that image to a URL. In my case, my URL is "Imager.aspx," which is my image handling page, and a querystring parameter set to the URL of the image I am linking, http://froogle.google.com/froogle/images/froogle_110tall.gif. If I did not want to link to an image, but use one from the file system, I can change my querystring parameter to be something else. Basically my app needs to determine what to send to the Imager.aspx, which processes the image.

My Imager.aspx.cs will determine if the image source I am sending it is a URL or a file reference. I can then load my image using the following.

Bitmap bmap = new Bitmap(
  System.Net.HttpWebRequest.Create(
  Request.QueryString["url"]).GetResponse().GetResponseStream() );
OR
Bitmap bmap = new Bitmap( Request.QueryString["file"] );

Obviously I would check the data for errors. Now we have loaded our image from either a URL or file, and we can manipulate it however we want. In my case I will flip the image using the following.

bmap.RotateFlip(RotateFlipType.Rotate180FlipY);
Bitmap bmapTemp = new Bitmap(500,100);
Graphics gfx = Graphics.FromImage( (System.Drawing.Image) bmapTemp);
gfx.DrawImage(bmap, new Rectangle(0,0,500,100) );

Finally we save the image and dispose of all objects.

bmap.Save( Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg );
bmap.Dispose();
bmapTemp.Dispose();
gfx.Dispose();

To demonstrate the effect of this test, I have used Javascript. The end result looks like this.

Wednesday, January 12, 2005 12:16:29 PM (Mountain Standard Time, UTC-07:00) | Comments [1] | #
Wednesday, January 12, 2005 10:49:07 PM (Mountain Standard Time, UTC-07:00)
In the example you provide, if an error occurs, the objects will never be disposed, resulting in a resource leak.

To make sure that this happens, you can use a try/finally block, and check each object for null before calling Dispose. The C# using keyword does this automatically.

Your snippet would look something like this:

using (Bitmap bmap = new Bitmap(System.Net.HttpWebRequest.Create(Request.QueryString["url"]).GetResponse().GetResponseStream()))
using (Bitmap tempBitmap = new Bitmap(500, 100))
using (Graphics g = Graphics.FromImage((Image)tempBitmap))
{
g.DrawImage(bmap, new Rectangle(0, 0, 500, 100));
bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
}
Comments are closed.
MuellerDesigns.net
Search
On This Page
The Split Personality of the Tester/Developer
Cross Site Scripting (XSS)
Creating files with FSUTIL
PowerShell Management Library for Hyper-V
Installing Windows 7
Installing Linux in Hyper-V
Internet Explorer 8 Release Candidate 1
PowerShell Documentation
Automate Daily Tasks with PowerShell
SketchPath XPath Editor
Software Testing - Revisited
Architecting Buildings and Software
NBCOlympics.com with Silverlight
Marker Interfaces and C# Attributes
Most Popular
JavaScript ReplaceAll Functionality
What is polymorphism?
What is composition?
Sorting with IComparable and IComparer
Applying the Observer Pattern in ASP.NET
MVP in ASP.NET
What is abstraction?
What is encapsulation?
What is a class?
What is inheritance?
Authentication in ASP.NET
Calendar Controls
XPathNavigator.CheckValidity new for 2.0
SQL Server 2005 Connection Issues
Auto-attach to process '[####] aspnet_wp.exe' on m...
What is an object?
FreeTextBox
VMWare and VPC
An Example of Reflection using C#
Changing File Ownership In Vista and Longhorn
Archive
Links
Categories
My Local Blog Map
Blogroll
About
Powered by:

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
MuellerDesigns.net

Sign In

Help Those In Need
The Hunger Site
Ronald McDonald House Charities (RMHC) of Western Washington & Alaska