Search code examples
c#graphicsgdicropdrawimage

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.

For example, I may have some images that are 16:9 that I I want resized and then cropped to 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.

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.

Essentially I just want to cut the sides off of an image (center it) that is wider than a 4:3 aspect. Obviously I'd cut the top and bottom of an image that is portrait instead of landscape.

Any help would be greatly appreciated.

TIA


Solution

  • 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.