[英]Resize image in C# with aspect ratio and crop central image so there are no gaps
[英]crop image to 4:3 aspect ratio c#
我的工作很艰难,无法解决所有纵横比大于4:3到4:3的图像。
例如,我可能需要II调整一些16:9图像,然后将其裁剪为4:3。
我已经可以使用调整大小位,但是它保持了相同的宽高比。 我知道我需要使用Graphics.DrawImage()
但我不完全确定参数应该是什么,也不知道如何导出这些参数。
这是我所知道的:
var dimension = (double)bigSide/smallSide
if(dimension > 1.4)
{
Graphics.DrawImage(resImage, new Rectangle(?, ?, ?, ?), ?, ?, ?, ?, GraphicsUnit.Pixel);
}
因此所有这些问号都是我不了解的参数。 我也不确定将图像缩减为4:3时需要什么样的数学运算。
本质上,我只想剪切比4:3宽的图像的边(居中)。 显然,我会剪切图像的顶部和底部,该图像是纵向而不是横向。
任何帮助将不胜感激。
TIA
我看到您评论过,您还想将裁剪后的图像调整为较小的尺寸,对吗?
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);
}
使用调整大小方法并提供宽度作为参数。 不需要增加高度,因为它是通过裁剪方法完成的。
这是一种用于裁剪更宽图像的方法,以便您可以理解该概念。
首先计算图像的额外宽度。 也就是说,占用的额外空间超过了4:3的相同空间。
考虑一下我想将1366 x 768图片裁剪为1024 x 768。
在这里,我们可以计算出高度(768px)的extra_width:
4 / 3 * (768) = 1024
因此,这将为您提供768高度的目标宽度。
现在,额外的宽度是:
1366 - 1024
现在,您可以通过将起始裁切点设置为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);
}
Rectangle sourceRectangle = new Rectangle(0, 0, 1600, 900);
Rectangle destinationRectangle = new Rectangle(0, 0, 800, 600);
此代码从0,0开始裁剪具有所需纵横比的图像部分。 您可以根据需要设置x坐标和y坐标,并相对于图像中心进行计算。 例如,如果您的图像的中心点是1600 x 900,则中心点是800,而中心点是450
Rectangle destinationRectangle = new Rectangle(400, 75, 800, 600);
这有些简化,但我希望您能明白这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.