我正在尝试做与链接问题相同的事情,但使用 C#。我正在显示一个缩放的图像,并允许用户选择要裁剪的区域。但是,我不能只从缩放的图像选择中获取 x1y1、x2y2 坐标并从原始图像中裁剪。我已经尝试像在另一个问题中那样做一些基本的数学运算,但这显然也不是正确的方法(它肯定更接近)。
编辑
原始图像尺寸:w = 1024 h = 768
缩放图像尺寸:w = 550 h = 412
我从一张图片开始,比如 1024x768。我希望它尽可能大地放入 550x550 的盒子中。我正在使用以下方法来获取缩放的图像大小(同时保持纵横比)。然后我对这些新尺寸进行基本调整。
至于选择区域,它可以是 (0,0) 到 (100,100) 的任何值。
private static Rectangle MaintainAspectRatio(Image imgPhoto, Rectangle thumbRect)
{
int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)thumbRect.Width / (float)sourceWidth);
nPercentH = ((float)thumbRect.Height / (float)sourceHeight);
//if we have to pad the height pad both the top and the bottom
//with the difference between the scaled height and the desired height
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((thumbRect.Width - (sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = (int)((thumbRect.Height - (sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Rectangle retRect = new Rectangle(thumbRect.X, thumbRect.Y, destWidth, destHeight);
return retRect;
}