如何将图像缩小到maxSize?

时间:2013-11-10 17:47:34

标签: c# image scale

我想要的是将尺寸大于480k像素的图片重新调整为480k像素或更低;

-- maxSize = 480k
-- picture1.Size = 1600*300 = 480k = maxSize => that's OK.
-- picture2.Size = 2600*200 = 520k => problem (should be reduced to maxSize or less).

picture2.Size / maxSize = 1.083

picture2.Size/1.083 = 480148 (~= maxSize) => that's OK.

我们假设1.083是图片大小调整的比例: 比率= 1.083;

如何应用此比率以保持图片宽高比?

2 个答案:

答案 0 :(得分:1)

您只想解决系统问题:

newWidth * newHeight = maxSize;
newWidth / newHeight = picture2.Width / picture2.Height

,解决方案是:

double newWidth = Math.Sqrt(picture2.Width * maxSize / picture2.Height);
double newHeight = Math.Sqrt(picture2.Height * maxSize / picture2.Width);
这样,picture2的宽高比将被保留,尺寸不会超过maxSize

答案 1 :(得分:0)

(2600-x)/(200-y)= 2600/200 和 x * y = 1.083

现在可以将图片重新调整为小于maxSize的大小,并且宽高比也保持不变。