【发布时间】:2020-06-15 20:48:21
【问题描述】:
我正在开发自己的图片查看器,并且正在创建图像裁剪方法。它确实适用于我当前的代码。但是,应用程序会动态调整图像大小以适应用户的屏幕。所以当它调整大小时,计算出来的图像的 X.Y 坐标是不正确的。我数学不是很好,所以我不知道如何计算。
这是我正在使用的代码
internal static Int32Rect GetCrop()
{
var cropArea = cropppingTool.CropTool.CropService.GetCroppedArea();
var x = Convert.ToInt32(cropArea.CroppedRectAbsolute.X);
var y = Convert.ToInt32(cropArea.CroppedRectAbsolute.Y);
var width = Convert.ToInt32(cropArea.CroppedRectAbsolute.Width);
var height = Convert.ToInt32(cropArea.CroppedRectAbsolute.Height);
return new Int32Rect(x, y, width, height);
}
cropArea 变量来自我自己修改后的https://github.com/dmitryshelamov/UI-Cropping-Image 版本。它是一个Rect,从用户绘制的正方形返回X和Y坐标以及宽度和高度,用于选择图像的裁剪区域。
我有调整后图像宽度和高度的变量,以及图像的原始像素宽度和像素高度。裁剪 UI 使用调整大小的变量,以适应用户的屏幕。
为清楚起见,图像大小是这样计算的,图像控件设置为 Stretch.Fill
double width = sourceBitmap.PixelWidth;
double height = sourceBitmap.PixelHeight;
double maxWidth = Math.Min(SystemParameters.PrimaryScreenWidth - 300, width);
double maxHeight = Math.Min(SystemParameters.PrimaryScreenHeight - 300, height);
var aspectRatio = Math.Min(maxWidth / width, maxHeight / height);
width *= aspectRatio;
height *= aspectRatio;
image.Width = width;
image.Height = height;
那么问题来了,如何计算渲染大小和实际像素大小之间的偏移量?
【问题讨论】:
标签: c# wpf math coordinates crop