Subscribe | Alerts via Email
View All Quotes
“You know you have achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away.”
-Antoine de Saint-Exupery
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

©2010 Cal Zant
Sign In
Total Posts: 106
This Year: 5
This Month: 1
This Week: 0
Comments: 2

This is a class I created to handle thumbnail generation and other typical image resizing tasks.  I think it is flexible and simple enough to be used in a variety of situations.  I will define the class and then give a couple of examples of how to use it.

public static class ImageProcessor
{
    /// <summary>
    /// Resizes the image located at the given originalRelativePath, making its longest side equal to the given maxSize 
///
(keeping it proportional), and then saves it to the location indicated by the saveToRelativePath parameter. /// </summary> public static void CreateThumbnail(string originalRelativePath, string saveToRelativePath, int maxSize) { Bitmap originalImage = new Bitmap(HttpContext.Current.Server.MapPath(originalRelativePath)); Bitmap newImage = CreateThumbnail(originalImage, maxSize);         
        FileStream thisFS = new new FileStream(HttpContext.Current.Server.MapPath(saveToRelativePath), FileMode.Create);
        n
ewImage.Save(thisFS, ImageFormat.Jpeg); originalImage.Dispose(); newImage.Dispose(); } /// <summary> /// Returns a Bitmap object that represents the image located at the given relativePath, resized so that it's
/// longest side is
equal to the given maxSize (keeping it proportional). /// </summary> public static Bitmap CreateThumbnail(string relativePath, int maxSize) { Bitmap originalImage = new Bitmap(HttpContext.Current.Server.MapPath(relativePath)); return CreateThumbnail(originalImage, maxSize); } /// <summary> /// Returns a Bitmap object (which is just a generic image object that isn't necessarily related to the .bmp
/// filetype) that is the
same image is the given originalImage, but scaled down to the given size. If it is
/// a vertical image the height would be equal to
the max size, and if it was a horizontal image the width would
/// be the maxSize ... but it always keeps the image proportional.
/// </summary> public static Bitmap CreateThumbnail(Bitmap originalImage, int maxSize) { float newWidth, newHeight; if (originalImage.Width >= originalImage.Height) { newWidth = maxSize; newHeight = (newWidth * (float)originalImage.Height) / (float)originalImage.Width; } else { newHeight = maxSize; newWidth = (newHeight * (float)originalImage.Width) / (float)originalImage.Height; } // Don't ever enlarge the picture ... if the user is trying to resize it to a size that is larger than
// the original, simply
return the same size image as the original with the border around it. if (newHeight >= (float)originalImage.Height || newWidth >= (float)originalImage.Width) { newHeight = (float)originalImage.Height; newWidth = (float)originalImage.Width; } Bitmap newBitmap = new Bitmap(originalImage, (int)newWidth, (int)newHeight); Rectangle recBorder = new Rectangle(0, 0, (int)newWidth - 1, (int)newHeight - 1); Graphics thisGO = Graphics.FromImage(newBitmap); thisGO.DrawRectangle(new Pen(Color.Black), recBorder); originalImage.Dispose(); return newBitmap; } }

You could call the code a variety a ways ... here are two examples or how it might be used:

string originalImagePath = Server.MapPath("/images/Original.jpg");
string thumbnailImagePath = Server.MapPath("/images/Thumbnail.jpg");
ImageProcessor.CreateThumbnail(originalImagePath, thumbnailImagePath, 300);

...

Bitmap thumbnail = ClassLibrary.ImageProcessor.CreateThumbnail(new Bitmap(thisFileUpload.PostedFile.InputStream), 50);
thumbnail.Save(thumbnailImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
Friday, October 12, 2007 8:15:06 AM (Central Standard Time, UTC-06:00)  #