我正在艰难地围绕如何计算裁剪任何高宽比超过4:3到4:3的图像的数学。
例如,我可能有一些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
答案 0 :(得分:6)
我看到你评论说你想要将裁剪后的图像调整到更小的尺寸吗?
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);
}
使用resize方法并提供width作为参数。无需添加高度,因为它是通过裁剪方法完成的。
答案 1 :(得分:1)
这是一种裁剪更宽图像的方法,以便您可以理解这个概念。
首先计算图像的额外宽度。即多少额外的空间占用的比例超过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);
}
答案 2 :(得分:0)
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);
这有点简化,但我希望你能明白这一点。