如何调整图片大小并保持宽高比?

3
我想调整图片大小使其符合 640x640 的尺寸,并保持宽高比。例如,如果这是原始图片:http://i.imgur.com/WEMCSyd.jpg我想要按照以下方式调整大小:http://i.imgur.com/K2BalOm.jpg以保持纵横比(基本上,图像总是位于中间,保持宽高比,其余空间保留为白色)。我尝试编写了一个 C#程序,其中包含以下代码:
Bitmap originalImage, resizedImage;

            try
            {
                using (FileStream fs = new FileStream(textBox1.Text, System.IO.FileMode.Open))
                {
                    originalImage = new Bitmap(fs);
                }

                int imgHeight = 640;
                int imgWidth = 640;

                if (originalImage.Height == originalImage.Width)
                {
                    resizedImage = new Bitmap(originalImage, imgHeight, imgWidth);
                }
                else
                {
                    float aspect = originalImage.Width / (float)originalImage.Height;
                    int newHeight;
                    int newWidth;

                    newWidth = (int)(imgWidth / aspect);
                    newHeight = (int)(newWidth / aspect);

                    if (newWidth > imgWidth || newHeight > imgHeight)
                    {
                        if (newWidth > newHeight)
                        {
                            newWidth = newHeight;
                            newHeight = (int)(newWidth / aspect);
                        }
                        else
                        {
                            newHeight = newWidth;
                            newWidth = (int)(newHeight / aspect);
                        }
                    }

                    resizedImage = new Bitmap(originalImage, newWidth, newHeight);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

但是它不符合我的需求。

@jmelosegui 感谢提供链接,但我真的不懂jQuery。这就是为什么我正在尝试用C#来实现它。 - user5204184
2个回答

3

假设你的图片大小为 (W, H)。令 s = max(W, H),然后将图片缩放至 (w, h) = (640 * W / s, 640 * H / s),其中 / 表示整数除法。请注意,我们有 w <= 640h <= 640max(w, h) = 640

在新的 (640, 640) 图像中,你的图片的水平和垂直偏移量分别为 x = (640 - W) / 2y = (640 - H) / 2

你可以通过创建一个新的空白白色图像 (640, 640),然后将当前图像绘制到矩形 (x, y, w, h) 中来完成所有这些操作。

var sourcePath = textBox1.Text;
var destinationSize = 640;
using (var destinationImage = new Bitmap(destinationSize, destinationSize))
{
    using (var graphics = Graphics.FromImage(destinationImage))
    {
        graphics.Clear(Color.White);
        using (var sourceImage = new Bitmap(sourcePath))
        {
            var s = Math.Max(sourceImage.Width, sourceImage.Height);
            var w = destinationSize * sourceImage.Width / s;
            var h = destinationSize * sourceImage.Height / s;
            var x = (destinationSize - w) / 2;
            var y = (destinationSize - h) / 2;

            // Use alpha blending in case the source image has transparencies.
            graphics.CompositingMode = CompositingMode.SourceOver;

            // Use high quality compositing and interpolation.
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

            graphics.DrawImage(sourceImage, x, y, w, h);
        }
    }
    destinationImage.Save(...);
}

1
您真的值得一枚奖章,不仅您解释了如何完成,还提供了代码。我喜欢您超越传统的思考方式。非常感谢! - user5204184
@user5204184 这里最重要的三点是:(1)首先在“纸上”解决问题,(2)除非绝对必要,否则尽量不要通过情况推理,(3)如果您的问题仅涉及整数且所有结果都应为整数,请尽量避免引入浮点数。 - Timothy Shields
非常感谢您的建议,我非常感激。下次我编写代码时一定会牢记这点。 - user5204184

-3

你能否在图片标签中添加max-width:100%。并使用CSS将父标签的固定宽度定义好。希望这样就可以了,不需要编写C#代码来实现。

Eg. <figure > <img src="" > </figure>

Css
Figure{ width:600px }
Img { max-width: 100%}

抱歉如果我听起来像个新手,但我不太懂CSS。你介意解释一下如何使用它,以及如何用它调整多张图片的大小吗? - user5204184

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接