简体   繁体   English

将图片裁剪为4:3的宽高比c#

[英]crop image to 4:3 aspect ratio c#

I'm having a tough time wrapping my head around how to figure out the math for cropping any image that has a higher aspect ratio than 4:3 to 4:3. 我的工作很艰难,无法解决所有纵横比大于4:3到4:3的图像。

For example, I may have some images that are 16:9 that II want resized and then cropped to 4:3. 例如,我可能需要II调整一些16:9图像,然后将其裁剪为4:3。

The resize bit I already have working, but it's maintaining the same aspect ratio. 我已经可以使用调整大小位,但是它保持了相同的宽高比。 I know I need to use Graphics.DrawImage() But I'm not entirely sure what the parameters should be nor how I derive those parameters. 我知道我需要使用Graphics.DrawImage()但我不完全确定参数应该是什么,也不知道如何导出这些参数。

Here's what I do know: 这是我所知道的:

var dimension = (double)bigSide/smallSide
if(dimension > 1.4)
{
  Graphics.DrawImage(resImage, new Rectangle(?, ?, ?, ?), ?, ?, ?, ?, GraphicsUnit.Pixel);
}

so all of those question marks are parameters that I do not understand. 因此所有这些问号都是我不了解的参数。 I also am not sure what the math would need to look like for cutting down the image to 4:3. 我也不确定将图像缩减为4:3时需要什么样的数学运算。

Essentially I just want to cut the sides off of an image (center it) that is wider than a 4:3 aspect. 本质上,我只想剪切比4:3宽的图像的边(居中)。 Obviously I'd cut the top and bottom of an image that is portrait instead of landscape. 显然,我会剪切图像的顶部和底部,该图像是纵向而不是横向。

Any help would be greatly appreciated. 任何帮助将不胜感激。

TIA TIA

I saw you've commented that you want also to resize the cropped image to a smaller size right? 我看到您评论过,您还想将裁剪后的图像调整为较小的尺寸,对吗?

Image resizeImg(Image img, int width)
    {
        // 4:3 Aspect Ratio. You can also add it as parameters
        double aspectRatio_X = 4;
        double aspectRatio_Y = 3;                        
        double targetHeight = Convert.ToDouble(width) / (aspectRatio_X / aspectRatio_Y);

        img = cropImg(img);
        Bitmap bmp = new Bitmap(width, (int)targetHeight);
        Graphics grp = Graphics.FromImage(bmp);
        grp.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
        return (Image)bmp;

    }

    Image cropImg(Image img)
    {
        // 4:3 Aspect Ratio. You can also add it as parameters
        double aspectRatio_X = 4;
        double aspectRatio_Y = 3;

        double imgWidth = Convert.ToDouble(img.Width);
        double imgHeight = Convert.ToDouble(img.Height);

        if (imgWidth / imgHeight > (aspectRatio_X / aspectRatio_Y))
        {
            double extraWidth = imgWidth - (imgHeight * (aspectRatio_X / aspectRatio_Y));
            double cropStartFrom = extraWidth / 2;
            Bitmap bmp = new Bitmap((int)(img.Width - extraWidth), img.Height);
            Graphics grp = Graphics.FromImage(bmp);                                                
            grp.DrawImage(img, new Rectangle(0, 0, (int)(img.Width - extraWidth), img.Height), new Rectangle((int)cropStartFrom, 0, (int)(imgWidth - extraWidth), img.Height), GraphicsUnit.Pixel);
            return (Image)bmp;
        }
        else
            return null;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox2.Image = resizeImg(pictureBox1.Image, 60);
    }

Use the resize method and provide width as parameter. 使用调整大小方法并提供宽度作为参数。 No need to add height because it is done by the crop method. 不需要增加高度,因为它是通过裁剪方法完成的。

Here's a method for cropping a wider image, so that you can understand the concept. 这是一种用于裁剪更宽图像的方法,以便您可以理解该概念。

First calculate the extra width of the image. 首先计算图像的额外宽度。 ie how much additional space it is taking more than a 4:3 ratio of the same. 也就是说,占用的额外空间超过了4:3的相同空间。

Consider I want to crop a 1366 x 768 picture to 1024 x 768. 考虑一下我想将1366 x 768图片裁剪为1024 x 768。

Here we can calculate the extra_width with the height (768px): 在这里,我们可以计算出高度(768px)的extra_width:

4 / 3 * (768) = 1024

So, that gives you the target width for a 768 height. 因此,这将为您提供768高度的目标宽度。

Now the extra width is: 现在,额外的宽度是:

1366 - 1024

Now you can crop the image by putting the starting crop point to 1/2 of the extra_width, and select the full_height. 现在,您可以通过将起始裁切点设置为extra_width的1/2来裁切图像,然后选择full_height。

  Image cropImg(Image img)
    {
        // 4:3 Aspect Ratio. You can also add it as parameters
        double aspectRatio_X = 4;
        double aspectRatio_Y = 3;

        double imgWidth = Convert.ToDouble(img.Width);
        double imgHeight = Convert.ToDouble(img.Height);

        if (imgWidth / imgHeight > (aspectRatio_X / aspectRatio_Y))
        {
            double extraWidth = imgWidth - (imgHeight * (aspectRatio_X / aspectRatio_Y));
            double cropStartFrom = extraWidth / 2;
            Bitmap bmp = new Bitmap((int)(img.Width - extraWidth), img.Height);
            Graphics grp = Graphics.FromImage(bmp);                                                
            grp.DrawImage(img, new Rectangle(0, 0, (int)(img.Width - extraWidth), img.Height), new Rectangle((int)cropStartFrom, 0, (int)(imgWidth - extraWidth), img.Height), GraphicsUnit.Pixel);
            return (Image)bmp;
        }
        else
            return null;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox2.Image = cropImg(pictureBox1.Image);
    }

Answers are here and here 答案在这里这里

Rectangle sourceRectangle = new Rectangle(0, 0, 1600, 900);

Rectangle destinationRectangle = new Rectangle(0, 0, 800, 600);

This code crops image part with required aspect ratio, starting from 0,0. 此代码从0,0开始裁剪具有所需纵横比的图像部分。 You may set x-coordinate and y-coordinate as you like, calculating it in relation to image center. 您可以根据需要设置x坐标和y坐标,并相对于图像中心进行计算。 For example if your image is 1600 x 900 centre point is 800 and 450 so to crop it's center 例如,如果您的图像的中心点是1600 x 900,则中心点是800,而中心点是450

Rectangle destinationRectangle = new Rectangle(400, 75, 800, 600);

This is somewhat simplified but I hope that you'll catch the point. 这有些简化,但我希望您能明白这一点。

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

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