Search code examples
delphifiremonkeyvcl

How do I identify the colour of a pixel at the location of a mouse click in Firemonkey?


I have done something similar in VCL. I'm by no means a professional, and I do not expect this to be the best way to do it, but here's what I had:

pt := TImage(Sender).ScreenToClient(Mouse.CursorPos);
color := image1.Canvas.Pixels[pt.X, pt.Y];

I'm basically looking for some assistance in effectively porting this code into firemonkey, to get the same result, considering images/canvases etc. seem to work a little differently and I'm pretty unfamiliar with it.

Thanks in advance.


Solution

  • I guess you need something like this:

    procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Single);
    var
      Cl: TAlphaColor;
      Dt: TBitmapData;
    begin
      if Image1.Bitmap.Map(TMapAccess.maRead, Dt) then
      begin
        Cl:= Dt.GetPixel(Trunc(X), Trunc(Y));
        Image1.Bitmap.Unmap(Dt);
      end;
    end;