如何调整图像大小并保持纵横比?

How to resize my image and maintain the aspect ratio?

我正在尝试调整图片大小以适合 640x640 大小并保持纵横比。

例如,如果这是原始图片:http://i.imgur.com/WEMCSyd.jpg 我想这样调整大小:http://i.imgur.com/K2BalOm.jpg保持纵横比(基本上,图像总是在中间,保持纵横比,其余space保持白色)

我试过用 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);
            }

但它没有按我需要的方式工作。

你能在图片标签中添加 max-width:100% 吗?并使用 css 定义父标签的固定宽度。希望这应该可以工作,无需为此编写 C# 代码。

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

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

(W, H) 为您的图片大小。让s = max(W, H)。然后你想将图像大小调整为 (w, h) = (640 * W / s, 640 * H / s) 其中 / 表示整数除法。请注意,我们有 w <= 640h <= 640 以及 max(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(...);
}