HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Image resizing class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
resizingimageclass

Problem

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 maxHeig

Solution

PascalCase the method names and method params if you are feeling overly ambitious.

// Set new width if in bounds
    if (onlyResizeIfWider)
    {
        if (ImageToResize.Width <= newWidth)
        {
            newWidth = ImageToResize.Width;
        }
    }


FindBugs barks in Java for the above behavior... refactor into a single if since you are not doing anything within the first if anyways...

// Set new width if in bounds
    if (onlyResizeIfWider && ImageToResize.Width <= newWidth)
    {
        newWidth = ImageToResize.Width;
    }


Comments here could be a bit more descriptive; while you state what the end result is I am still lost as to why that would resolve the issue.

// Prevent using images internal thumbnail
    ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);


Maybe something similar to what is stated on this blog...

// Prevent using images internal thumbnail since we scale above 200px; flipping
    // the image twice we get a new image identical to the original one but without the    
    // embedded thumbnail
    ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

Code Snippets

// Set new width if in bounds
    if (onlyResizeIfWider)
    {
        if (ImageToResize.Width <= newWidth)
        {
            newWidth = ImageToResize.Width;
        }
    }
// Set new width if in bounds
    if (onlyResizeIfWider && ImageToResize.Width <= newWidth)
    {
        newWidth = ImageToResize.Width;
    }
// Prevent using images internal thumbnail
    ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
// Prevent using images internal thumbnail since we scale above 200px; flipping
    // the image twice we get a new image identical to the original one but without the    
    // embedded thumbnail
    ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

Context

StackExchange Code Review Q#549, answer score: 12

Revisions (0)

No revisions yet.