Recent

Author Topic: screen capture of forms  (Read 20037 times)

clemmi

  • Jr. Member
  • **
  • Posts: 54
screen capture of forms
« on: January 01, 2014, 11:36:40 pm »
How can I capture the image of the main form (or other form) or a TStringGrid only, instead of the whole monitor screen?

When I use:
---  Mybitmap := TBitmap.Create;
---  MyDC := GetDC(0);
---  MyBitmap.LoadFromDevice(MyDC);
---  ReleaseDC(0, MyDC);
---  Image1.picture := TPicture(MyBitmap);

I get the screen capture of the whole monitor in my TImage.

If I change to:
---  MyDC := GetDC(Self.Handle);

Then I get a black image on my TImage.

Is there a way to do it?
Thanks.
« Last Edit: January 01, 2014, 11:42:21 pm by clemmi »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: screen capture of forms
« Reply #1 on: January 01, 2014, 11:50:11 pm »
try Self.PaintTo() and see if that helps.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: screen capture of forms
« Reply #2 on: January 02, 2014, 12:03:32 am »
Hi there. I use the following procedure to capture the image of a calender on a form & save it to PNG file. You can slightly modify the code so it saves as Bitmap or Jpeg if you wish. It works very well.

Code: [Select]
procedure TfrmCalendar.bbtnPrintCalendarClick(Sender: TObject);
(* Prints the calendar *)
var
  MyPNG: TPortableNetworkGraphic;
  ScreenDC: HDC;
  strSaveFileAs: string;
begin
  MyPNG := TPortableNetworkGraphic.Create;
  try
    ScreenDC := GetDC(Self.Handle);
    MyPNG.LoadFromDevice(ScreenDC);
    MyPNG.Width := Round(Self.Width);
    MyPNG.Height := Round(Self.Height);
    ReleaseDC(0, ScreenDC);

    // Get the name of the file to be created from the user
    strSaveFileAs := GetFileName('png', UTF8ToAnsi(strSaveDialogTitle), strWorkingDirectory);
    if Trim(strSaveFileAs) <> '' then
      MyPNG.SaveToFile(strSaveFileAs);
  finally
    MyPNG.Free;
  end;
end;

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: screen capture of forms
« Reply #3 on: January 02, 2014, 12:33:39 am »
I just created a new project with a TButton and TImage and the following code:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  d:THandle;
begin
  //d:=GetDC(Self.Handle);//<--- Client Area
  d:=GetWindowDC(Self.Handle);//<--- Whole Form
  Image1.Picture.Bitmap.LoadFromDevice(d);
  ReleaseDC(Self.Handle, d);
end;

It worked.

clemmi

  • Jr. Member
  • **
  • Posts: 54
Re: screen capture of forms
« Reply #4 on: January 02, 2014, 07:21:01 pm »
Thank you for all the responses. I tried all of them and this is what worked for me:
 ;D

 MySCRcap : THandle; 
procedure TForm1.Button1Click(Sender: TObject);
 begin
  MySCRcap := GetDC(Form1.StringGrid1.Handle);       
  Image1.Picture.Bitmap.LoadFromDevice(MySCRcap);
  ReleaseDC(Self.Handle, MySCRcap);
 end;

I click on the button and the StringGrid is displayed on the TImage.
I can take it from here to make it on a separate window and save it in HD.

For me "Get DC(self.handle)" gives me the image of the button.
"GetWindowDC" is not recognized. Is it Delphi instead of Lazarus? Not listed with CTRL-Space Bar.
"GetFormImage" says I have the wrong number of parameters...
Similar with other sugestions.

Thanks. All's well that ends well.



engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: screen capture of forms
« Reply #5 on: January 03, 2014, 03:56:14 am »
Glad that you solved the problem.I want to let you know about a few things:

MySCRcap : THandle; 
procedure TForm1.Button1Click(Sender: TObject);
begin
Since MySCRcap is used only inside the procedure you can put it inside it:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  MySCRcap : THandle; 
begin

To release the DC handle after using GetDC or GetWindowDC, you should use pass the same window handle that you used. The following code:
  MySCRcap := GetDC(Form1.StringGrid1.Handle);       
  Image1.Picture.Bitmap.LoadFromDevice(MySCRcap);
  ReleaseDC(Self.Handle, MySCRcap);
 end;
Should be changed to:
Quote
  MySCRcap := GetDC(Form1.StringGrid1.Handle);       
  Image1.Picture.Bitmap.LoadFromDevice(MySCRcap);
  ReleaseDC(Form1.StringGrid1.Handle, MySCRcap);
 end;

"GetWindowDC" is not recognized.
Add Windows to your uses section:
Code: [Select]
uses
 ..., Windows;

clemmi

  • Jr. Member
  • **
  • Posts: 54
Re: screen capture of forms
« Reply #6 on: January 03, 2014, 03:05:23 pm »
1.- got it.
2.- I already realized that just after the posting. Thnks.
3.- Is there a way to find out which units should be added in 'uses' in cases like this?
Something like using ALT-F11 to see which units have to be added.
-cl

clemmi

  • Jr. Member
  • **
  • Posts: 54
Re: screen capture of forms
« Reply #7 on: January 03, 2014, 03:22:37 pm »
To save the image using SaveDialog, which extension should I use?

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: screen capture of forms
« Reply #8 on: January 03, 2014, 05:53:56 pm »
To save the image using SaveDialog, which extension should I use?

You use whatever extension you created the image in. Since it seems you used Bitmap, you use the "bmp" extension. In my example above, I created a PNG image so I used a "png" extension.

JD
« Last Edit: January 03, 2014, 05:56:49 pm by JD »
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: screen capture of forms
« Reply #9 on: January 03, 2014, 06:43:53 pm »
You can use any registered extension you like. I'm aware of three of them bmp, jpg, and png.

Code: [Select]
  Image1.Picture.SaveToFile('test.jpg');
  Image1.Picture.SaveToFile('test.png');
  Image1.Picture.SaveToFile('test.bmp');

For finer details when using jpg, you can use properties from Image1.Picture.Jpeg.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: screen capture of forms
« Reply #10 on: January 14, 2014, 09:11:33 am »
Quote
To save the image using SaveDialog, which extension should I use?
Anything recognized by TPicture. Currently this seems: Bitmap, BNPG, PNM, XPM, JPEG. Read TPicture documentation/interface for further information.

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: screen capture of forms
« Reply #11 on: January 14, 2014, 09:58:12 am »
I use the following procedure to capture the image of a calender on a form & save it to PNG file.
Does that only work for Windows targets (LCL-Win*), or can that code be used for LCL-GTK and LCL-QT widgetsets too?
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: screen capture of forms
« Reply #12 on: January 14, 2014, 10:22:51 am »
I use the following procedure to capture the image of a calender on a form & save it to PNG file.
Does that only work for Windows targets (LCL-Win*), or can that code be used for LCL-GTK and LCL-QT widgetsets too?

Yes GetDC has been written to work on most lcl widgetsets (GTK,QT,COCOA, WIN-32 & 64)) as long as you use lclintf, lcltype and friends and not widgetse specific units like windows you should be ok.

Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: screen capture of forms
« Reply #13 on: January 15, 2014, 08:24:39 pm »
Thanks!
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

McClane

  • New Member
  • *
  • Posts: 44
Re: screen capture of forms
« Reply #14 on: August 06, 2018, 07:15:37 am »
Hi, excuse me, why when people post code don't include the uses clause !

 

TinyPortal © 2005-2018