ColdFusion & .NET: Resizing the Width of an Image

This is my first post comparing some functionality and code between ColdFusion and .NET.  For all of posts in this series I am running ColdFusion 8 Enterprise and the 3.5 .NET Framework.  All of the sample code I will be showing for .NET is written in C#.  Sorry if you were looking for VB examples!

Today's post covers a basic piece of functionality that I use quite often, resizing the width of an image.  For example, a user uploads a photo taken from their digital camera that is 1024 x 700.  We need to resize the original image into a nice little thumbnail image that is 250 pixels wide.

First the ColdFusion code.  I would like to point out that prior to ColdFusion 8 it was necessary to use a third party component tag to resize an image, or use some sort of Java library.  In version 8, Adobe added the CFIMAGE tag, making this task extremely easy.

<code lang="cfm[lines]"><cfimage action="resize" destination="#resizedFile#" height="" overwrite="yes" source="#sourceFile#" width="250"></cfimage>

Notice in the example above that since the height is left blank it will resize the image to the specified width letting the height change to what it needs to be based on the width.

Now for the .NET code.  This task is still fairly easy in .NET, but as you can see requires a lot more code.  I have placed this code in a function, so it's easy to reuse without having to copy the code throughout your application.

Below is the function itself:

<code lang="csharp[lines]">static void ResizeImageByWidth(string orgName, string resizeName, int resizeWidth)<br></br>
{<br></br> String src = orgName;<br></br> String dest = resizeName;<br></br> int thumbWidth = resizeWidth;

//Get the source image to a System.Drawing.Image object
System.Drawing.Image image = System.Drawing.Image.FromFile(src);

//Create a System.Drawing.Bitmap with the desired width and height of the thumbnail.
int srcWidth = image.Width;
int srcHeight = image.Height;

Decimal sizeRatio = ((Decimal)srcHeight / srcWidth);
int thumbHeight = Decimal.ToInt32(sizeRatio * thumbWidth);
Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);

//Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);

//Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

//Set the System.Drawing.Graphics object property InterpolationMode to High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

//Draw the original image into the target Graphics object scaling to the desired width and height
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);

//Save to destination file

//dispose / release resources

Calling the function is easy, just pass in your arguments:

<code lang="csharp[lines]">ResizeImageByHeight(sourceName, resizedImageName, 250);

And there you have it.  You can now resize an image based on a width in ColdFusion and .NET.


comments powered by Disqus