// you're reading...

.NET

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.

action = "resize"
height = ""
width = "250"
source = "#sourceFile#"
destination = "#resizedFile#"
overwrite = "yes"
>

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:

static void ResizeImageByWidth(string orgName, string resizeName, int resizeWidth)
{
String src = orgName;
String dest = resizeName;
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
bmp.Save(dest);

//dispose / release resources
bmp.Dispose();
image.Dispose();
}

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

ResizeImageByHeight(sourceName, resizedImageName, 250);

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

Discussion

10 Responses to “ColdFusion & .NET: Resizing the Width of an Image”

  1. Will that handle jpg and png? Also does the method take a file location to a file ie c:wwwimagesimage.jpg or do you have to read in the image before the re size? Also did you time the 2 if so which is faster?

    Posted by James | February 6, 2009, 11:32 PM
  2. Will that handle jpg and png? Also does the method take a file location to a file ie c:wwwimagesimage.jpg or do you have to read in the image before the re size? Also did you time the 2 if so which is faster?

    Posted by James | February 6, 2009, 6:32 PM
  3. James,
    I have not tried having .NET resize a PNG file. When I get a few spare minutes I’ll give it a try. As for the path it accepts the file location on disk like c:somedirsomefile.jpg.
    I have not timed the two to see which if faster. I am using the exact .NET code above on a console app that resizes around 5000 images and it runs to completion in a little less than a minute.

    Posted by Mike Fleming | February 7, 2009, 1:27 AM
  4. James,
    I have not tried having .NET resize a PNG file. When I get a few spare minutes I’ll give it a try. As for the path it accepts the file location on disk like c:somedirsomefile.jpg.
    I have not timed the two to see which if faster. I am using the exact .NET code above on a console app that resizes around 5000 images and it runs to completion in a little less than a minute.

    Posted by Mike Fleming | February 6, 2009, 8:27 PM
  5. ResizeImageByWidth, very good function, I can use that in an application I am writing at this time.

    Posted by .Net Development | February 7, 2009, 3:06 AM
  6. ResizeImageByWidth, very good function, I can use that in an application I am writing at this time.

    Posted by .Net Development | February 6, 2009, 10:06 PM
  7. Just a question? What is the difference in server overhead for each of these code segments? I am just curios as cfimage is a real processor hog. I love CF but I am starting to explore other options for resizing images on the fly because in a high load environment I am starting to see a drop off of performance. Just curious which one uses less memory and processor time…

    Posted by Daniel Sellers | March 9, 2009, 11:39 PM
  8. Just a question? What is the difference in server overhead for each of these code segments? I am just curios as cfimage is a real processor hog. I love CF but I am starting to explore other options for resizing images on the fly because in a high load environment I am starting to see a drop off of performance. Just curious which one uses less memory and processor time…

    Posted by Daniel Sellers | March 9, 2009, 6:39 PM
  9. Daniel,
    I just posted a new entry comparing the performance difference. Results were surprising, with a big nod to .NET.

    Posted by Mike Fleming | March 10, 2009, 12:29 PM
  10. Daniel,
    I just posted a new entry comparing the performance difference. Results were surprising, with a big nod to .NET.

    Posted by Mike Fleming | March 10, 2009, 7:29 AM

Post a comment

Categories