简体   繁体   English

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

[英]How to resize my image and maintain the aspect ratio?

I'm trying to resize my picture so it fits the 640x640 size and keep the aspect ratio. 我正在尝试调整图片大小,使其适合640x640的尺寸并保持宽高比。

For example, if this is the original picture: http://i.imgur.com/WEMCSyd.jpg I want to resize in this way: http://i.imgur.com/K2BalOm.jpg to keep the aspect ratio (Basically, the image is always in the middle, and keeps the aspect ratio, the rest of space remains white) 例如,如果这是原始图片: http : //i.imgur.com/WEMCSyd.jpg我想以这种方式调整大小: http : //i.imgur.com/K2BalOm.jpg以保持宽高比(基本上,图像始终在中间,并保持宽高比,其余空间保持白色)

I have tried making a program in C# which has this code: 我试图用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);
            }

But it doesn't work the way I need it to. 但这并不符合我的需要。

Let (W, H) be the size of your image. (W, H)为图像大小。 Let s = max(W, H) . s = max(W, H) Then you want to resize the image to (w, h) = (640 * W / s, 640 * H / s) where / denotes integer division. 然后,您要将图像调整为(w, h) = (640 * W / s, 640 * H / s) ,其中/表示整数除法。 Note that we have w <= 640 and h <= 640 and max(w, h) = 640 . 请注意,我们有w <= 640h <= 640max(w, h) = 640

The horizontal and vertical offsets for your image inside of the new (640, 640) image are x = (640 - W) / 2 and y = (640 - H) / 2 , respectively. 在新(640, 640)图像中(640, 640)图像的水平和垂直偏移分别为x = (640 - W) / 2y = (640 - H) / 2

You can accomplish all of this by creating a new (640, 640) blank white image and then drawing your current image to the rectangle (x, y, w, h) . 您可以通过创建新的(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(...);
}

Can u add max-width:100% to image tag. 您可以在图片标签中添加max-width:100%吗? And define your fixed width to parent tag using css. 并使用CSS将固定宽度定义为父标记。 Hope this should work no need to write c# code for the same. 希望这应该工作而无需编写相同的C#代码。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM