简体   繁体   English

调整图像大小会生成具有原始尺寸的图像

[英]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;
}

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): 您的代码只有一个小问题(请参阅我在Stream上的评论,尤其是在newImage声明中):

    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, 结果是这样

在此处输入图片说明

The original image size is 820 x 760 原始图像尺寸为820 x 760

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM