In the TImageViewer control, the user can zoom or pan the picture.
My question is, when the user clicks on the picture, how to get the user's click position on the picture? Especially after the user can zoom in, zoom out or pan the picture, how to get the corresponding picture click position?
As shown below:
How to know whether the user clicked on the battery position?
Demo Project: Demo source code
I didn't test but it should work:
procedure TfmMain.ivImageViewerMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
var
DX, DY: Single;
ImageX, ImageY: Single;
begin
if ivImageViewer.Bitmap.Width * ScalePicture >= ivImageViewer.Width then
DX = ivImageViewer.ViewportPosition.X
else
DX := (ivImageViewer.Bitmap.Width * ScalePicture - ivImageViewer.Width)/2;
ImageX := (X + DX) / ScalePicture;
if ivImageViewer.Bitmap.Height * ScalePicture >= ivImageViewer.Height then
DY = ivImageViewer.ViewportPosition.Y
else
DY := (ivImageViewer.Bitmap.Height * ScalePicture - ivImageViewer.Height)/2;
ImageY := (Y + DY) / ScalePicture;
end;
ImageX
and ImageY
are the coordinates relative to the original (unscaled) image.