imagedelphifiremonkey

In TImageViewer, how to get the position where the user clicks the picture?


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: enter image description here How to know whether the user clicked on the battery position?

Demo Project: Demo source code


Solution

  • 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.