【问题标题】:Calculate the visible area in Firemonkey's TImageViewer when zoom?缩放时计算Firemonkey的TImageViewer中的可见区域?
【发布时间】:2022-11-21 23:29:51
【问题描述】:

TImageViewer 组件提供图像缩放功能,并在必要时显示水平和垂直滚动条。考虑到缩放和可见图像区域,如何计算源图像中的坐标? (例如,当点击进入 TImageViewer 时)。我缺少 ViewPortSize 属性。 ViewPortPosition 正确返回图像的左上角,但我无法在任何地方找到高度和宽度。

以下解决方案仅在未放大图像时有效。当我放大图像并显示滚动条时(图像右下角点不可见),此解决方案必须考虑当前视口的大小:

procedure TfmxFirebaseDemo.imvAnotateFileMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Single);
var
  Offset: TPointF;
  Point: TPoint;
  Relative: TPointF;
  Scale: single;
begin
  Scale := 1; // imvAnotateFile.ViewportSize.X or Y in relation to current width/height;
  Offset.X := imvAnotateFile.Width - imvAnotateFile.ContentBounds.Width * Scale +
    imvAnotateFile.ViewPortPosition.X;
  Offset.Y := imvAnotateFile.Height - imvAnotateFile.ContentBounds.Height * Scale +
    imvAnotateFile.ViewPortPosition.Y;
  Point.X := round(X - Offset.X);
  Point.Y := round(Y - Offset.Y);
  Relative.X := Point.X / (imvAnotateFile.Bitmap.Width * imvAnotateFile.BitmapScale);
  Relative.Y := Point.Y / (imvAnotateFile.Bitmap.Height * imvAnotateFile.BitmapScale);
  FMX.Types.Log.d('Pos %d, %d Relative %f, %f Scale %f', [Point.X, Point.Y, Relative.X, Relative.Y, Scale]);
end;

【问题讨论】:

  • 目前还不清楚(无论是从描述还是示例代码)你到底想计算什么。
  • @BrakNicku:感谢您的提示:我已经更改了文本中的第二句:考虑到缩放,我如何计算可见图像区域中图像中的坐标? (例如,当点击进入 TImageViewer 时)
  • 因此,如果您想将客户端转换为位图坐标,那么this answer 就是这样做的。首先,它计算位图的左上角坐标(偏移量可以在两个方向上,具体取决于位图的大小和查看器),平移鼠标点,最后缩放。
  • 我什至在打开这个线程之前就看到了这个解决方案。不幸的是,当我放大并滚动到图像中时,此解决方案无法正确运行。
  • 这个答案中的数学看起来不错,但现在我也测试了它并针对不同的 BitmapScaleViewPortPosition 值得到了正确的结果,所以如果它不能正确运行对您来说,查看修改后的代码和获得的示例与预期结果对比会很有用。

标签: delphi firemonkey


【解决方案1】:

您可以使用以下代码获取视口大小:

var
  ViewportSize: TPointF;
begin
  ViewportSize := TPointF.Create(imvAnotateFile.Width, imvAnotateFile.Height);
  ViewportSize := ViewportSize.Scale(1 / imvAnotateFile.BitmapScale);
end;

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2017-10-09
    • 2013-11-27
    • 2011-06-19
    相关资源
    最近更新 更多