1

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?

Arioch 'The
  • 15,799
  • 35
  • 62
Alberto Rossi
  • 1,800
  • 4
  • 36
  • 58
  • 1
    Why did you use TImage and TLabel. Much better to paint direct to a canvas. – David Heffernan Dec 25 '13 at 20:57
  • 2
    Much easier, though, to draw the grid once in Paint, and then never have to paint anything else in the program, @David. I'd much prefer to assign labels' Caption properties and let Delphi draw the text than to write the layout and text-drawing code myself. Were this my project, I'd probably forego the grid image and just assign the labels' background colors to create the same grid effect. Or use a real grid control. – Rob Kennedy Dec 25 '13 at 21:10
  • 2
    Alberto, did you really not find any examples of making screen shots? I can understand not seeing any examples of getting specific areas of the screen, but once you get the image of the entire screen, you can use ordinary image-editing functions to extract a specific area of the image, regardless of the original source of the image. – Rob Kennedy Dec 25 '13 at 21:13
  • 2
    @Rob, Lazarus is more benevolent in publishing canvas, so if you just put all the controls on a borderless panel, you can really just copy its canvas content. It's not that easy in Delphi. – TLama Dec 25 '13 at 21:20
  • I didn't use canvas because, as @RobKennedy said, I preferred make that with Paint. It's faster. – Alberto Rossi Dec 25 '13 at 21:25
  • Wait until you need to change the layout, or have font scaling? What if a user prefers larger fonts? – David Heffernan Dec 25 '13 at 22:36

2 Answers2

5

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;
TLama
  • 75,147
  • 17
  • 214
  • 392
4

You can use GetFormImage to get the form image, and keep the part that corresponds to your image area in it:

var
  Bmp: TBitmap;
begin
  Bmp := GetFormImage;
  try
    Bmp.Canvas.CopyRect(Image1.ClientRect, Bmp.Canvas, Image1.BoundsRect);
    Bmp.SetSize(Image1.Width, Image1.Height);
    Bmp.SaveToFile('....');
  finally
    Bmp.Free;
  end;
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169