c# - 在缩放模式下平移矩形位置 Picturebox 导致负 Y 坐标
问题描述
我正在使用以下类 Ref -在缩放模式图片框中转换矩形位置 将位图中的矩形选择转换为在 UI 中的图片框上绘制的框架控件。
public class ZoomFactor
{
public ZoomFactor() { }
public PointF TranslateZoomPosition(PointF coordinates, SizeF containerSize, SizeF imageSize)
{
PointF imageOrigin = TranslateCoordinatesOrigin(coordinates, containerSize, imageSize);
float scaleFactor = GetScaleFactor(containerSize, imageSize);
return new PointF(imageOrigin.X / scaleFactor, imageOrigin.Y / scaleFactor);
}
public RectangleF TranslateZoomSelection(RectangleF selectionRect, SizeF containerSize, SizeF imageSize)
{
PointF selectionTrueOrigin = TranslateZoomPosition(selectionRect.Location, containerSize, imageSize);
float scaleFactor = GetScaleFactor(containerSize, imageSize);
SizeF selectionTrueSize = new SizeF(selectionRect.Width / scaleFactor, selectionRect.Height / scaleFactor);
return new RectangleF(selectionTrueOrigin, selectionTrueSize);
}
public RectangleF TranslateSelectionToZoomedSel(RectangleF selectionRect, SizeF containerSize, SizeF imageSize)
{
float scaleFactor = GetScaleFactor(containerSize, imageSize);
RectangleF zoomedSelectionRect = new
RectangleF(selectionRect.X * scaleFactor, selectionRect.Y * scaleFactor,
selectionRect.Width * scaleFactor, selectionRect.Height * scaleFactor);
PointF imageScaledOrigin = GetImageScaledOrigin(containerSize, imageSize);
zoomedSelectionRect.Location = new PointF(zoomedSelectionRect.Location.X + imageScaledOrigin.X,
zoomedSelectionRect.Location.Y + imageScaledOrigin.Y);
return zoomedSelectionRect;
}
public PointF TranslateCoordinatesOrigin(PointF coordinates, SizeF containerSize, SizeF imageSize)
{
PointF imageOrigin = GetImageScaledOrigin(containerSize, imageSize);
return new PointF(coordinates.X - imageOrigin.X, coordinates.Y - imageOrigin.Y);
}
public PointF GetImageScaledOrigin(SizeF containerSize, SizeF imageSize)
{
SizeF imageScaleSize = GetImageScaledSize(containerSize, imageSize);
return new PointF((containerSize.Width - imageScaleSize.Width) / 2,
(containerSize.Height - imageScaleSize.Height) / 2);
}
public SizeF GetImageScaledSize(SizeF containerSize, SizeF imageSize)
{
float scaleFactor = GetScaleFactor(containerSize, imageSize);
return new SizeF(imageSize.Width * scaleFactor, imageSize.Height * scaleFactor);
}
internal float GetScaleFactor(SizeF scaled, SizeF original)
{
return (original.Width > original.Height) ? (scaled.Width / original.Width)
: (scaled.Height / original.Height);
}
}
// Using the Class
RectangleF BitmapRect = ZoomHelper.TranslateSelectionToZoomedSel(ResizedRect, new SizeF(pbox_preview.Width, pbox_preview.Height), bit.Size);
这适用于大多数输入,但是当提供以下输入时,返回的 Y 坐标为负并且缩放错误。
ResizedRect --> {X = 37 Y = 2 Width = 227 Height = 308}
PictureBoxSize ---> SizeF(603,423)
imagesize ---> (311,310)
The returned rectangle is this
{X = 71.73955 Y = -85.15273 Width = 440.131836 Height = 597.1833}
Y 坐标为负。
解决方案
我实际上并没有尝试你的代码,所以这只是一个蓝色的镜头:也许,在你的函数中GetScaleFactor
你不想比较original.Width > original.Height
但是original.Width/original.Height > scaled.Width/scaled.Height
推荐阅读
- python - 如何合并具有重叠列的两个数据框,更新现有值并添加新值
- python - crypt python库中的河豚支持
- swift - 错误:CoreData:错误:无法在 NSManagedObject 类“任务”上调用指定的初始化程序
- python - 我可以使用什么 GEKKO 模型构造或建模策略来修复特定时间 MV 的 UPPER 值甚至状态属性
- php - 在 PHP 中使用 YouTube 数据 API 在后台上传视频
- tinylog - Tinylog:在 Json 编写器上获取“LOGGER ERROR:非法严重级别”
- docker - 如何使用 Artifactory 中的 docker 映像创建 pod
- python - SciPy 插值给出了意想不到的曲线
- reactjs - lottie 文件降低 NextJS 应用程序的性能
- python - 使用 python 多处理访问和修改二维数组