使矩形的区域适应不同的大小
Adapt Rectangle's region to a different size
在 Windows 表单下,我截取了具有特定 window 大小的特定 window 的全尺寸屏幕截图,我将其保存到位图对象中,然后,我声明用于裁剪该位图区域的矩形结构,因为稍后我只需要操作非常具体的 part/region 屏幕截图...
为了简化这个问题,假设 window 位图大小为 640x480,矩形的 X、Y 为:436,150,宽度、高度为:146,170,我从屏幕截图(位图)是气球图像。 window 是一个视频游戏。
问题是,当 window 尺寸增加时,气球图像也会增加,很明显,所以 window 尺寸的矩形的 x、y 和 width/height当游戏 window 的尺寸更大时,640x480 的尺寸将无法 capture/crop 整个气球图像...
我需要知道如何计算 x,y width/height,当 window 大小发生变化时,我的矩形应该正确裁剪气球图像。我需要调整矩形。
所以,如果这是我拥有的预定义大小和矩形:
{ new Size(640, 480), new Rectangle(436, 150, 146, 170) }
据此,在 window 大小为 800x600 和 1280x768 的情况下,矩形应该正确裁剪相同等效区域的近似适应值或多或少是这些:
{ new Size(800, 600), new Rectangle(546, 186, 186, 212) }
{ new Size(1280, 768), new Rectangle(830, 232, 240, 274) }
...只是近似值,但并不完美,因为我是手动完成的,因为我不确定计算和自动化此数学运算的方法是什么。
我希望我的问题得到理解。提前谢谢你。
试试这个:
如果宽度为 640:
X = 436 / 640 = 0.68125 (68.125%)
W = 146 / 640 = 0.22125 (22.125%)
如果身高 480:
Y = 150 / 480 = 0.3125 (31.25%)
H = 170 / 480 = 0.3541666666666666666666666667 (35.41666666666666666666666667%)
考虑表格的大小为this.Width
,高度为this.Height
:
decimal pX = 0.68125;
decimal pW = 0.22125;
decimal pY = 0.3125;
decimal pH = 0.3541666666666666666666666667;
Rectangle rect = new Rectangle(this.Width * pX, this.Height * pY, this.Width * pW, this.Height * pH);
也许你想多了,但你需要做的就是捕获原始大小和新大小(对于 X 和 Y)之间的百分比变化,然后将该百分比应用于属性原始矩形得到新矩形。
例如:
public static Rectangle GetNewRectangle(Size oldSize, Rectangle oldRectangle,
Size newSize)
{
var percentChangeX = (double)newSize.Width / oldSize.Width;
var percentChangeY = (double)newSize.Height / oldSize.Height;
return new Rectangle
{
X = (int)(oldRectangle.X * percentChangeX),
Y = (int)(oldRectangle.Y * percentChangeY),
Width = (int)(oldRectangle.Width * percentChangeX),
Height = (int)(oldRectangle.Height * percentChangeY)
};
}
用法示例:
// Helper method to display size and rectangle properties
private static string GetDisplayValues(Size size, Rectangle rect)
{
return $" - size: {size.Width} x {size.Height}\n" +
$" - rect: {rect.X}, {rect.Y} : {rect.Width} x {rect.Height}\n";
}
private static void Main()
{
var size = new Size(640, 480);
var rect = new Rectangle(436, 150, 146, 170);
Console.WriteLine($"Original:\n{GetDisplayValues(size, rect)}");
var newSize = new Size(800, 600);
var newRect = GetNewRectangle(size, rect, newSize);
Console.WriteLine($"Resized:\n{GetDisplayValues(newSize, newRect)}");
GetKeyFromUser("\nDone! Press any key to exit...");
}
输出
给定源位图和边界内的选择矩形:
RectangleF SourceRect = new Rectangle(Point.Empty, SourceBitmap.Size);
Rectangle SelectionRect = new Rectangle([Point], [Size]);
当 SourceBitmap
更改其大小时,选择矩形的新大小是使用 SourceBitmap
的旧大小和新大小之间的关系给出的比例因子计算的:
RectangleF DestinationRect = new RectangleF(Point.Empty, InflatedBitmap.Size);
SizeF ScaleFactor = new SizeF(DestinationRect.Width / SourceRect.Width,
DestinationRect.Height / SourceRect.Height);
PointF NewPosition = new PointF(SelectionRect.X * ScaleFactor.Width, SelectionRect.Y * ScaleFactor.Height);
SizeF NewSize = new SizeF(SelectionRect.Width * ScaleFactor.Width, SelectionRect.Height * ScaleFactor.Height);
RectangleF InflatedSelection = new RectangleF(NewPosition, NewSize);
使用 SourceBitmap 和大小为:
的选择矩形
RectangleF SourceRect = new RectangleF(0, 0, 640, 480);
RectangleF SelectionRect = new RectangleF(436, 150, 146, 170);
如果膨胀位图的大小为:
RectangleF DestinationRect1 = new RectangleF(0, 0, 800, 600);
RectangleF DestinationRect2 = new RectangleF(0, 0, 1280, 768);
比例因子为 (1.25, 1.25)
和 (2, 1.6)
的 Inflated 选择将是(向下舍入):
RectangleF InflatedSelection1 = new RectangleF(545, 187, 182, 212);
RectangleF InflatedSelection2 = new RectangleF(872, 240, 292, 272);
在 Windows 表单下,我截取了具有特定 window 大小的特定 window 的全尺寸屏幕截图,我将其保存到位图对象中,然后,我声明用于裁剪该位图区域的矩形结构,因为稍后我只需要操作非常具体的 part/region 屏幕截图...
为了简化这个问题,假设 window 位图大小为 640x480,矩形的 X、Y 为:436,150,宽度、高度为:146,170,我从屏幕截图(位图)是气球图像。 window 是一个视频游戏。
问题是,当 window 尺寸增加时,气球图像也会增加,很明显,所以 window 尺寸的矩形的 x、y 和 width/height当游戏 window 的尺寸更大时,640x480 的尺寸将无法 capture/crop 整个气球图像...
我需要知道如何计算 x,y width/height,当 window 大小发生变化时,我的矩形应该正确裁剪气球图像。我需要调整矩形。
所以,如果这是我拥有的预定义大小和矩形:
{ new Size(640, 480), new Rectangle(436, 150, 146, 170) }
据此,在 window 大小为 800x600 和 1280x768 的情况下,矩形应该正确裁剪相同等效区域的近似适应值或多或少是这些:
{ new Size(800, 600), new Rectangle(546, 186, 186, 212) }
{ new Size(1280, 768), new Rectangle(830, 232, 240, 274) }
...只是近似值,但并不完美,因为我是手动完成的,因为我不确定计算和自动化此数学运算的方法是什么。
我希望我的问题得到理解。提前谢谢你。
试试这个:
如果宽度为 640:
X = 436 / 640 = 0.68125 (68.125%)
W = 146 / 640 = 0.22125 (22.125%)
如果身高 480:
Y = 150 / 480 = 0.3125 (31.25%)
H = 170 / 480 = 0.3541666666666666666666666667 (35.41666666666666666666666667%)
考虑表格的大小为this.Width
,高度为this.Height
:
decimal pX = 0.68125;
decimal pW = 0.22125;
decimal pY = 0.3125;
decimal pH = 0.3541666666666666666666666667;
Rectangle rect = new Rectangle(this.Width * pX, this.Height * pY, this.Width * pW, this.Height * pH);
也许你想多了,但你需要做的就是捕获原始大小和新大小(对于 X 和 Y)之间的百分比变化,然后将该百分比应用于属性原始矩形得到新矩形。
例如:
public static Rectangle GetNewRectangle(Size oldSize, Rectangle oldRectangle,
Size newSize)
{
var percentChangeX = (double)newSize.Width / oldSize.Width;
var percentChangeY = (double)newSize.Height / oldSize.Height;
return new Rectangle
{
X = (int)(oldRectangle.X * percentChangeX),
Y = (int)(oldRectangle.Y * percentChangeY),
Width = (int)(oldRectangle.Width * percentChangeX),
Height = (int)(oldRectangle.Height * percentChangeY)
};
}
用法示例:
// Helper method to display size and rectangle properties
private static string GetDisplayValues(Size size, Rectangle rect)
{
return $" - size: {size.Width} x {size.Height}\n" +
$" - rect: {rect.X}, {rect.Y} : {rect.Width} x {rect.Height}\n";
}
private static void Main()
{
var size = new Size(640, 480);
var rect = new Rectangle(436, 150, 146, 170);
Console.WriteLine($"Original:\n{GetDisplayValues(size, rect)}");
var newSize = new Size(800, 600);
var newRect = GetNewRectangle(size, rect, newSize);
Console.WriteLine($"Resized:\n{GetDisplayValues(newSize, newRect)}");
GetKeyFromUser("\nDone! Press any key to exit...");
}
输出
给定源位图和边界内的选择矩形:
RectangleF SourceRect = new Rectangle(Point.Empty, SourceBitmap.Size);
Rectangle SelectionRect = new Rectangle([Point], [Size]);
当 SourceBitmap
更改其大小时,选择矩形的新大小是使用 SourceBitmap
的旧大小和新大小之间的关系给出的比例因子计算的:
RectangleF DestinationRect = new RectangleF(Point.Empty, InflatedBitmap.Size);
SizeF ScaleFactor = new SizeF(DestinationRect.Width / SourceRect.Width,
DestinationRect.Height / SourceRect.Height);
PointF NewPosition = new PointF(SelectionRect.X * ScaleFactor.Width, SelectionRect.Y * ScaleFactor.Height);
SizeF NewSize = new SizeF(SelectionRect.Width * ScaleFactor.Width, SelectionRect.Height * ScaleFactor.Height);
RectangleF InflatedSelection = new RectangleF(NewPosition, NewSize);
使用 SourceBitmap 和大小为:
RectangleF SourceRect = new RectangleF(0, 0, 640, 480);
RectangleF SelectionRect = new RectangleF(436, 150, 146, 170);
如果膨胀位图的大小为:
RectangleF DestinationRect1 = new RectangleF(0, 0, 800, 600);
RectangleF DestinationRect2 = new RectangleF(0, 0, 1280, 768);
比例因子为 (1.25, 1.25)
和 (2, 1.6)
的 Inflated 选择将是(向下舍入):
RectangleF InflatedSelection1 = new RectangleF(545, 187, 182, 212);
RectangleF InflatedSelection2 = new RectangleF(872, 240, 292, 272);