How does this class to resize an image look?
using System;
using System.Collections.Generic;
using System.Web;
using System.Drawing;
using System.IO;
/*
* Resizes an image
**/
public static class ImageResizer
{
// Saves the image to specific location, save location includes filename
private static void saveImageToLocation(Image theImage, string saveLocation)
{
// Strip the file from the end of the dir
string saveFolder = Path.GetDirectoryName(saveLocation);
if (!Directory.Exists(saveFolder))
{
Directory.CreateDirectory(saveFolder);
}
// Save to disk
theImage.Save(saveLocation);
}
// Resizes the image and saves it to disk. Save as property is full path including file extension
public static void resizeImageAndSave(Image ImageToResize, int newWidth, int maxHeight, bool onlyResizeIfWider, string thumbnailSaveAs)
{
Image thumbnail = resizeImage(ImageToResize, newWidth, maxHeight, onlyResizeIfWider);
thumbnail.Save(thumbnailSaveAs);
}
// Overload if filepath is passed in
public static void resizeImageAndSave(string imageLocation, int newWidth, int maxHeight, bool onlyResizeIfWider, string thumbnailSaveAs)
{
Image loadedImage = Image.FromFile(imageLocation);
Image thumbnail = resizeImage(loadedImage, newWidth, maxHeight, onlyResizeIfWider);
saveImageToLocation(thumbnail, thumbnailSaveAs);
}
// Returns the thumbnail image when an image object is passed in
public static Image resizeImage(Image ImageToResize, int newWidth, int maxHeight, bool onlyResizeIfWider)
{
// Prevent using images internal thumbnail
ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
// Set new width if in bounds
if (onlyResizeIfWider)
{
if (ImageToResize.Width <= newWidth)
{
newWidth = ImageToResize.Width;
}
}
// Calculate new height
int newHeight = ImageToResize.Height * newWidth / ImageToResize.Width;
if (newHeight > maxHeight)
{
// Resize with height instead
newWidth = ImageToResize.Width * maxHeight / ImageToResize.Height;
newHeight = maxHeight;
}
// Create the new image
Image resizedImage = ImageToResize.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
// Clear handle to original file so that we can overwrite it if necessary
ImageToResize.Dispose();
return resizedImage;
}
// Overload if file path is passed in instead
public static Image resizeImage(string imageLocation, int newWidth, int maxHeight, bool onlyResizeIfWider)
{
Image loadedImage = Image.FromFile(imageLocation);
return resizeImage(loadedImage, newWidth, maxHeight, onlyResizeIfWider);
}
}