在C#中调整图像大小 [英] Resizing images in C#

查看:106
本文介绍了在C#中调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的方法,我在网上找到,而重新调整图像以近似大小,同时保持宽高比。

I have the below method which I found online,which resizes an image to an approximate size, while keeping the aspect ratio.

     public Image ResizeImage(Size size)
    {
        int sourceWidth = _Image.Width;
        int sourceHeight = _Image.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)size.Width / (float)sourceWidth);
        nPercentH = ((float)size.Height / (float)sourceHeight);

        if (nPercentH > nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.DrawImage(_Image, 0, 0, destWidth, destHeight);
        g.Dispose();

        return (Image)b;
    }



我通常在大小通与100像素的宽度和100px的高度 - 因为我要求的一部分,我不能有任何单一维度(高度或宽度)100px的下之中,因此,如果宽高比不是方形另一个尺寸会更高。

I usually pass in a size with a width of 100px and a height of 100px - as part of my requirements I can't have any single dimension (height or width) being under 100px, so if the aspect ratio isn't square the other dimension would be higher.

我正在用这种方法发现是偶然的尺寸将是100像素下的一个 - 比如96PX或99px。我怎样才能改变这种方法,以确保不会发生这种情况?

What I'm finding with this method is occasionally one of the dimensions will be under 100px - such as 96px or 99px. How can I change this method to ensure this doesn't happen?

推荐答案

中的代码仅仅是不合适的。它没有得分点使用浮点运算,具有四舍五入的错误的方式,因此您可以轻松地与99像素,而不是100。最终总是青睐整数运算,所以你可以控制舍入的诀窍。它只是没有做任何事情,以确保维度之一是足够大的,拥有96个像素结束的方式。只要写更好的代码。这样的:

The code is just inappropriate. It doesn't score points for using floating point math, that has a knack for rounding the wrong way so you can easily end up with 99 pixels instead of 100. Always favor integer math so you can control the rounding. And it just doesn't do anything to ensure that one of the dimensions is large enough, the way to end up with 96 pixels. Just write better code. Like:

    public static Image ResizeImage(Image img, int minsize) {
        var size = img.Size;
        if (size.Width >= size.Height) {
            // Could be: if (size.Height < minsize) size.Height = minsize;
            size.Height = minsize;
            size.Width = (size.Height * img.Width + img.Height - 1) / img.Height;
        }
        else {
            size.Width = minsize;
            size.Height = (size.Width * img.Height + img.Width - 1) / img.Width;
        }
        return new Bitmap(img, size);
    }



我留下了评论,以显示你做什么,如果你只是想使确保图像足够大,接受更大的图像。这是不是从问题的明确。如果是这样的话,那么复制,如果else子句中的语句为好。

I left a comment to show what you do if you are only want to make sure the image is large enough and accept larger images. It wasn't clear from the question. If that's the case then replicate that if statement in the else clause as well.

这篇关于在C#中调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆