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

时间:2015-08-08 03:13:35

标签: c# image image-resizing

我正在尝试调整图片大小以使其符合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);
            }

但它不能按我需要的方式工作。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:-2)

可以将max-width:100%添加到图片代码中。并使用css将固定宽度定义为父标记。希望这应该工作,不需要编写相同的c#代码。

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

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