Search code examples
delphiscreenshotlazarusfpc

Take a screenshot of a particular area


I am using Lazarus and I have a TImage inside a form. The black table is a TImage and the numbers are labels. I need to take a screenshot of the red area I drew.

enter image description here

How can I perform this?

I have Lazarus 1.0.14 and I didn't find any example about this. Any suggestion?


Solution

  • This is a painful design, but well, one simple way might be to put all the controls on a common container and copy its canvas to a bitmap. The following example assumes, that you have put your image and all the labels on a common TPanel control (Panel1):

    procedure TForm1.Button1Click(Sender: TObject);
    var
      R: TRect;
      Bitmap: TBitmap;
    begin
      Bitmap := TBitmap.Create;
      try
        R := Rect(0, 0, Panel1.Width, Panel1.Height);
        Bitmap.SetSize(Panel1.Width, Panel1.Height);
        Bitmap.Canvas.CopyRect(R, Panel1.Canvas, R);
        Bitmap.SaveToFile('C:\Screenshot.bmp');
      finally
        Bitmap.Free;
      end;
    end;