C# 从缩放的小尺寸图像中获取相同的矩形位置
我正在尝试处理一个大尺寸的图像。由于处理需要花费太多的时间来完成,我正在处理之前调整图像的大小。处理后,我正在小尺寸图像上绘制一个矩形。我如何将此矩形的坐标转换为原始未缩放图像,即:在未缩放图像上的相同位置绘制矩形形象 我使用下面的代码来调整图像的大小C# 从缩放的小尺寸图像中获取相同的矩形位置,c#,math,geometry,gdi+,system.drawing,C#,Math,Geometry,Gdi+,System.drawing,我正在尝试处理一个大尺寸的图像。由于处理需要花费太多的时间来完成,我正在处理之前调整图像的大小。处理后,我正在小尺寸图像上绘制一个矩形。我如何将此矩形的坐标转换为原始未缩放图像,即:在未缩放图像上的相同位置绘制矩形形象 我使用下面的代码来调整图像的大小 public static Size ResizeKeepAspect(Size CurrentDimensions, int maxWidth, int maxHeight) { int newHeight = CurrentDimen
public static Size ResizeKeepAspect(Size CurrentDimensions, int maxWidth, int maxHeight)
{
int newHeight = CurrentDimensions.Height;
int newWidth = CurrentDimensions.Width;
if (maxWidth > 0 && newWidth > maxWidth) //WidthResize
{
Decimal divider = Math.Abs((Decimal)newWidth / (Decimal)maxWidth);
newWidth = maxWidth;
newHeight = (int)Math.Round((Decimal)(newHeight / divider));
}
if (maxHeight > 0 && newHeight > maxHeight) //HeightResize
{
Decimal divider = Math.Abs((Decimal)newHeight / (Decimal)maxHeight);
newHeight = maxHeight;
newWidth = (int)Math.Round((Decimal)(newWidth / divider));
}
return new Size(newWidth, newHeight);
}
这就是我想要达到的目标
这是一个简单的关系计算。例如:
Image A 100 (w) x 100 (h): Pixel x = 10, y = 30
Image B 200 (w) x 200 (h): Pixel x = a, y = b
10 / 100 (w) = a / 200 (w)
200 (w) * 10 / 100 (w) = a //Image B's x value
a = 20
30 / 100 (h) = b / 200 (h)
200 (h) * 30 / 100 (h) = b //Image A's y value
b = 60
@我需要一些现有的代码来调整图像的大小。目前只显示大小计算。我会检查并返回给您…谢谢在计算缩放点时您为什么有+0.5?@HarryBloom将值四舍五入。
Rectangle ConvertToLargeRect(Rectangle smallRect, Size largeImageSize, Size smallImageSize)
{
double xScale = (double)largeImageSize.Width / smallImageSize.Width;
double yScale = (double)largeImageSize.Height / smallImageSize.Height;
int x = (int)(smallRect.X * xScale + 0.5);
int y = (int)(smallRect.Y * yScale + 0.5);
int right = (int)(smallRect.Right * xScale + 0.5);
int bottom = (int)(smallRect.Bottom * yScale + 0.5);
return new Rectangle(x, y, right - x, bottom - y);
}