裁剪图像为4:3的宽高比C# [英] crop image to 4:3 aspect ratio c#

查看:286
本文介绍了裁剪图像为4:3的宽高比C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个艰难的时间环绕我的头围绕如何找出数学用于裁剪具有更高的纵横比大于4的任何图像:3至4:3

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.

例如,我可能有一些图片是16:9的II要调整大小,然后裁剪为4:3

For example, I may have some images that are 16:9 that I I want resized and then cropped to 4:3.

调整大小位我已经有工作,但它保持相同的高宽比。我知道我需要使用 Graphics.DrawImage()但我不能完全肯定的参数应该是什么,也不怎么得到这些参数。

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.

这是我所知道的:

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



所以所有这些问号的是我不明白的参数。我也不知道是什么数学需要像对图像削减为4:3

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. Obviously I'd cut the top and bottom of an image that is portrait instead of landscape.

任何帮助将不胜感激。

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.

这篇关于裁剪图像为4:3的宽高比C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆