screen capture of forms lazarus / 39-Graphics ---------------------------------------------------------------------- clemmi · 2014-01-01 22:36 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. ---------------------------------------------------------------------- taazz · 2014-01-01 22:50 try Self.PaintTo() and see if that helps. ---------------------------------------------------------------------- JD · 2014-01-01 23:03 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 ---------------------------------------------------------------------- engkin · 2014-01-01 23:33 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 · 2014-01-02 18:21 Thank you for all the responses. I tried all of them and this is what worked for me:  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 · 2014-01-03 02:56 Glad that you solved the problem.I want to let you know about a few things: Quote from: clemmi on January 02, 2014, 07:21:01 pm 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: Quote from: clemmi on January 02, 2014, 07:21:01 pm  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; Quote from: clemmi on January 02, 2014, 07:21:01 pm"GetWindowDC" is not recognized. Add Windows to your uses section: Code: [Select]uses  ..., Windows; ---------------------------------------------------------------------- clemmi · 2014-01-03 14:05 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 · 2014-01-03 14:22 To save the image using SaveDialog, which extension should I use? ---------------------------------------------------------------------- JD · 2014-01-03 16:53 Quote from: clemmi on January 03, 2014, 03:22:37 pmTo 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 ---------------------------------------------------------------------- engkin · 2014-01-03 17:43 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 · 2014-01-14 08:11 QuoteTo 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 · 2014-01-14 08:58 Quote from: JD on January 02, 2014, 12:03:32 amI 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? ---------------------------------------------------------------------- taazz · 2014-01-14 09:22 Quote from: Graeme Geldenhuys on January 14, 2014, 09:58:12 amQuote from: JD on January 02, 2014, 12:03:32 amI 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. ---------------------------------------------------------------------- Graeme · 2014-01-15 19:24 Thanks! ---------------------------------------------------------------------- McClane · 2018-08-06 05:15 Hi, excuse me, why when people post code don't include the uses clause ! ---------------------------------------------------------------------- Thaddy · 2018-08-06 05:28 Quote from: McClane on August 06, 2018, 07:15:37 amHi, excuse me, why when people post code don't include the uses clause ! Because most people still use Windows and on Windows it isn't necessary to use lclintf. For other widgets sets, Lazarus actually mimics (some) Windows behavior through lclintf. That makes porting your code to other platforms easier. In most cases it is transparent. For Windows that would mean lclintf is an empty unit. (Which it probably is...) ---------------------------------------------------------------------- BeanzMaster · 2018-08-06 07:55 Hi for taking a screenshot of a form you can simply use the Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TForm.GetFormImage Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var bmp : TBitmap;  bmp := Form1.GetFormImage ---------------------------------------------------------------------- Weiss · 2023-08-28 05:57 Quote from: clemmi on January 03, 2014, 03:22:37 pmTo save the image using SaveDialog, which extension should I use? you can use any extension. I just tried, created image as .png and saved it as bitmap.