我正在开发一个应用程序来操纵在宽图像扫描仪上扫描的图像。这些图像在Canvas
上显示为ImageBrush
。 在这个Canvas
他们可以用鼠标制作Rectangle
来定义一个要裁剪的区域。调整绘制的矩形以适应原始图像
这里我的问题是根据原始图像大小调整Rectangle
的大小,以便裁剪原始图像上的确切区域。
到目前为止,我已经尝试了很多东西,它只是用我的大脑来找出正确的解决方案。
我知道我需要得到原始图像比画布上显示的图像更大的百分比。
原始图像的dimentions为:
H:5606
宽:7677
当我显示图像,它们分别是:
h:1058,04
w:1910
其中给出这些数字:
float percentWidth = ((originalWidth - resizedWidth)/originalWidth) * 100;
float percentHeight = ((originalHeight - resizedHeight)/originalHeight) * 100;
percentWidth = 75,12049
percentHeight = 81,12665
在这里,我找不出如何正确调整Rectangle
,以适应原始图像。
我最后的办法是这样的:
int newRectWidth = (int)((originalWidth * percentWidth)/100);
int newRectHeight = (int)((originalHeight * percentHeight)/100);
int newRectX = (int)(rectX + ((rectX * percentWidth)/100));
int newRectY = (int)(rectY + ((rectY * percentHeight)/100));
希望有人会导致我在正确的方向,因为我偏离轨道的在这里,我不能看见我错过了什么。
解决方案
private System.Drawing.Rectangle FitRectangleToOriginal(
float resizedWidth,
float resizedHeight,
float originalWidth,
float originalHeight,
float rectWidth,
float rectHeight,
double rectX,
double rectY)
{
// Calculate the ratio between original and resized image
float ratioWidth = originalWidth/resizedWidth;
float ratioHeight = originalHeight/resizedHeight;
// create a new rectagle, by resizing the old values
// by the ratio calculated above
int newRectWidth = (int)(rectWidth * ratioWidth);
int newRectHeight = (int)(rectHeight * ratioHeight);
int newRectX = (int)(rectX * ratioWidth);
int newRectY = (int)(rectY * ratioHeight);
return new System.Drawing.Rectangle(newRectX, newRectY, newRectWidth, newRectHeight);
}
哦,该死的你!其实这是我尝试的第一种方法,但我一定做错了什么,因为现在它工作!谢谢 :) –