【问题标题】:How to resize my image and maintain the aspect ratio?如何调整我的图像大小并保持纵横比?
【发布时间】:2015-08-08 03:13:34
【问题描述】:

我正在尝试调整图片大小,使其适合 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# 实现它的原因。

标签: c# image image-resizing


【解决方案1】:

(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(...);
}

【讨论】:

  • 你,先生,真的值得一枚奖章。您不仅解释了它是如何完成的,而且还提供了代码。我喜欢你跳出框框思考的方式。非常感谢!
  • @user5204184 这里最重要的三点是(1)首先“在纸上”解决问题,(2)除非绝对必要,否则尽量不要按案例推理,以及(3)如果你的问题只是涉及整数,所有结果都应该是整数,尽量避免将浮点数带入混合中。
  • 非常感谢您的建议。我非常感激。下次我尝试编写代码时,我一定会记住这一点。
【解决方案2】:

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

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

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

【讨论】:

  • 对不起,如果我听起来像个菜鸟,但我不太了解 CSS。你介意解释一下如何使用它,以及如何用它调整多张图片的大小吗?
猜你喜欢
  • 2012-11-15
  • 2019-08-10
  • 2013-06-26
  • 2012-04-15
  • 2012-05-01
相关资源
最近更新 更多