给定最小宽度和宽度时,获取图像的新图像尺寸。最小高度

时间:2011-10-21 17:28:57

标签: c# asp.net image css

我正在寻找一个将接受Image对象,最小宽度和最小高度的函数,它将返回一个大小对象(或具有width和height属性的任何其他对象)与新的维度图像。

例如,如果我传递的原始尺寸为:

的图像
  

1024x768(宽x高)

使用以下函数进入此函数:

  

最小宽度:256,最小高度:206

该函数将返回一个size对象:

  

新宽度:274,新高度:206

这允许图像填充空间,同时保留图像的纵横比。

感谢任何人都能给予的帮助。

干杯,

克里斯

1 个答案:

答案 0 :(得分:1)

Size GetMinimalDimensions(int origwidth, int origheight, int minwidth, int minheight)
{
    double scale1, scale2;
    scale1 = (double)origwidth / minwidth;
    scale2 = (double)origheight / minheight;
    if (scale1 > scale2) {
        return new Size((int)Math.Round(origwidth / scale2), minheight);
    }
    return new Size(minwidth, (int)Math.Round(origheight / scale1));
}

非常简单。为什么你自己不能这样做? OO