调整后的图像尺寸大于原始图像

时间:2015-02-10 04:38:02

标签: c# .net system.drawing image-size

在重新调整平均尺寸为1200x700的图像时,我遇到了一个有趣的问题。我正在使用以下方法来做同样的事情。我看到原始图像大小为210KB,输出图像大小为255KB。

    public static Image ResizeImage(Image image, Size size, bool preserveAspectRatio = true)
    {
        int newWidth;
        int newHeight;
        if (preserveAspectRatio)
        {
            int originalWidth = imageFile.Width;
            int originalHeight = imageFile.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(newImage))
        {
            graphicsHandle.CompositingQuality = CompositingQuality.HighQuality;
            graphicsHandle.SmoothingMode = SmoothingMode.HighQuality;
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;

            graphicsHandle.DrawImage(imageFile, 0, 0, newWidth, newHeight);
        }
        return newImage;
    }

要调用上面的方法我正在使用以下代码:

        WebClient wc = new WebClient();
        try
        {
            using (var image = Image.FromStream(new MemoryStream(wc.DownloadData(remoteFileLocation))))
            {
                Image resized = ResizeImage(image, new Size(660, 660));
                resized.Save(saveFileLocation);
            }
        }
        catch(Exception)
        {
            //todo
        }

我尝试了不同的InterpolationMode和CompositingQuality,但结果始终相同。是否有任何我想要减少图像尺寸和尺寸的东西?我的意思是,我期待如果尺寸减小,图像尺寸也会减小。

0 个答案:

没有答案