Shakeel Hussain Mir
Shakeel Hussain Mir

Reputation: 286

Image resize results in an image with original dimensions

I am trying to resize images. This code results in images with the original dimensions (no resize is performed).

Image original = Image.FromFile(Server.MapPath("~/SavedFiles/Audios/") + fileNameF);
Image resized = ResizeImage(original, new Size(200, 200));
MemoryStream memStream = new MemoryStream();
resized.Save(memStream, ImageFormat.Jpeg);

On button click, I am calling this code:

public static Image ResizeImage(Image image, Size size, bool preserveAspectRatio = true)
{
    int newWidth;
    int newHeight;

    if (preserveAspectRatio)
    {
        int originalWidth = image.Width;
        int originalHeight = image.Height;
        float percentWidth = (float)size.Width / (float)originalWidth;
        float percentHeight = (float)size.Height / (float)originalHeight;
        float percent = percentHeight < percentWidth ? percentHeight : percentWidth;

        newWidth = (int)(originalWidth * percent);
        newHeight = (int)(originalHeight * percent);
    }
    else
    {
        newWidth = size.Width;
        newHeight = size.Height;
    }

    Image newImage = new Bitmap(newWidth, newHeight);

    using (Graphics graphicsHandle = Graphics.FromImage(image))
    {
        graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphicsHandle.DrawImage(newImage, 0, 0, newWidth, newHeight);
    }

    return newImage;
}

Upvotes: 0

Views: 1310

Answers (1)

Ian
Ian

Reputation: 30813

This is how I do it based on your code, I tested is using my image in the file and locate it in the specified folder too. It works fine. Your code only have but small issues (see my comments on the Stream and especially in the newImage declaration):

    private void button1_Click(object sender, EventArgs e) {
        Image original = Image.FromFile(Application.StartupPath + "\\ChessSet_Orig.JPG");
        Image resized = ResizeImage(original, new Size(200, 200));          
        FileStream fileStream = new FileStream(Application.StartupPath + "\\ChessSet_resized.JPG", FileMode.Create); //I use file stream instead of Memory stream here
        resized.Save(fileStream, ImageFormat.Jpeg);
        fileStream.Close(); //close after use
    }

    public static Image ResizeImage(Image image, Size size, bool preserveAspectRatio = true) {
        int newWidth;
        int newHeight;
        if (preserveAspectRatio) {
            int originalWidth = image.Width;
            int originalHeight = image.Height;
            float percentWidth = (float)size.Width / (float)originalWidth;
            float percentHeight = (float)size.Height / (float)originalHeight;
            float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
            newWidth = (int)(originalWidth * percent);
            newHeight = (int)(originalHeight * percent);
        } else {
            newWidth = size.Width;
            newHeight = size.Height;
        }
        Image newImage = new System.Drawing.Bitmap(image, newWidth, newHeight); // I specify the new image from the original together with the new width and height
        using (Graphics graphicsHandle = Graphics.FromImage(image)) {
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphicsHandle.DrawImage(newImage, 0, 0, newWidth, newHeight);
        }
        return newImage;
    }

Here is the result,

enter image description here

The original image size is 820 x 760

enter image description here

Upvotes: 2

Related Questions