delphi 计算缩放时Firemonkey的TImageViewer中的可见区域?

xzlaal3s  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(190)

TImageViewer组件提供图像缩放功能,并在必要时显示水平和垂直滚动条。如何在考虑缩放和可见图像区域的情况下计算源图像中的坐标?(例如,当单击TImageViewer时)。我缺少ViewPortSize属性。ViewPortPosition正确返回图像的左上角,但我在任何地方都找不到高度和宽度。
以下解决方案仅在未放大图像时有效。当我放大图像并显示滚动条时(图像右下角点不可见),此解决方案必须考虑当前 windows 的大小:

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;
kzmpq1sx

kzmpq1sx1#

您可以使用下列程式码取得视埠大小:

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

相关问题