Form image copy lazarus / 7-LCL ---------------------------------------------------------------------- lfrodrigues · 2007-08-30 16:29 I'm porting a Delphi app into Lazarus. I need to copy a form's image (like a printscreen) into a bitmap file. In Delphi i used  aBitmap := GetFormImage; but I don't have that in Lazarus. I can not use aBitmap.Canvas.CopyRect(tmpR, Form.Canvas, tmpR); since if the form is offscreen or has windows on top all will be copied. Thanks in advance. Luís Rodrigues ---------------------------------------------------------------------- antonio · 2007-08-30 18:14 I am not sure if Lazarus does not have a GetFormImage method. Anyway, you can make a feature request on BugGTracker. ---------------------------------------------------------------------- cyber_python · 2007-08-30 18:15 just create a form and add a button and timage (and a memo and anything else ypu like) and copy paste this on Unit1 : unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Buttons,   StdCtrls, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)     Button1: TButton;     Image1: TImage;     Memo1: TMemo;     procedure Button1Click(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1; implementation { TForm1 } procedure copyImage(form : TForm; destination : TBitmap); //procedure that assigns the screenshot of "form" to "destination" var tmp : TBitmap; w,h : integer; r : TRect; begin   w := form1.Width;   h:= form1.Height;   r:= Rect(0,0,w,h);   tmp:=TBitmap.Create;   tmp.Width:=w;   tmp.Height:=h;   tmp.Canvas.CopyRect(r, form1.Canvas, r);   destination.Assign(tmp); end; procedure TForm1.Button1Click(Sender: TObject); begin   copyImage(form1, image1.Picture.Bitmap);   image1.Picture.SaveToFile('D:\img001.bmp'); end; initialization   {$I unit1.lrs} end. ---------------------------------------------------------------------- cyber_python · 2007-08-30 18:18 Sorry, i didn't read all of your post..... ---------------------------------------------------------------------- lfrodrigues · 2007-08-30 18:35 At least not with that name (GetFormImage). I just need to know how to copy the image from the form (or any other object) and I can do the patch my self. ---------------------------------------------------------------------- antonio · 2007-08-30 18:41 Then make your patch (diff file) and commit it on BugTracker. Thanks. ---------------------------------------------------------------------- lfrodrigues · 2007-08-30 19:41 I still need to know how to copy an form/control content :-p ---------------------------------------------------------------------- antonio · 2007-08-30 19:44 cyber_python's code can help you? ---------------------------------------------------------------------- lfrodrigues · 2007-08-31 01:40 Nop As I wrote above, when the window has something on top or if offscreen canvas.copyrect copies everything. Basically it copies what is visible to the user even if it's not part of the form (offscreen copies white) This happens also in Delphi so I think thats the expected behavior. Didn't try in linux to check if it  works. ---------------------------------------------------------------------- antonio · 2007-08-31 12:22 I never tried that. If you use canvas methods, probably it will work on Linux. ---------------------------------------------------------------------- cyber_python · 2007-08-31 13:11 The delphi implementation of GetFormImage (this code is most probably copyright to Borland so use it at your own risk  javascript:emoticon(':wink:') Wink  ) Removed code. If it is probably copyrighted, don't copy it on our Forum. You will have to work out how to implement the PainTo method - no Lazarus equivalent for TWinControl.PaintTo - anyway TForm is not a descendant of TWinControl..... Trying to solve this I found out something pretty bizzare : if I put the "windows" unit in my uses clause then I cannot use TBitmap as it thinks I mean the Bitmap structure in declared struct.inc..... Does anyone know how to solve this? ---------------------------------------------------------------------- lfrodrigues · 2007-08-31 16:41 Quote Trying to solve this I found out something pretty bizzare : if I put the "windows" unit in my uses clause then I cannot use TBitmap as it thinks I mean the Bitmap structure in declared struct.inc..... Does anyone know how to solve this? That also happened to me, just change the order of the uses clauses. uses ...,Graphics, Windows, ... fails uses ..., Windows, Graphis, ... works Since TBitmap class is defined in Graphics and TBitmap structure is defined in Windows the last included unit is used. ---------------------------------------------------------------------- Vincent Snijders · 2007-08-31 18:32 I guess you didn't take the course "Advanced topics in Pascal" :-). If the scoping rules bring another identifier into scope than you want, you can a higher element to specify the scope. This works with fields in records, but also with identifiers in units (UnitName.Identifier). So you can use Windows.TBitmap and Graphics.TBitmap. ---------------------------------------------------------------------- cyber_python · 2007-08-31 19:03 It works!!!! Thanks! ---------------------------------------------------------------------- windy · 2007-09-12 00:46 Hi I am also wanting to replace getformimage the code provied in the copyimage procedure works but It grabs any window on top I have tried form1.Canvas.Refresh; but that does now work how does getformimage pull it off? (PM me any info, thanks!!) ---------------------------------------------------------------------- windy · 2007-09-15 02:37 I have managed to get the form background working (and it does not capture what is on top of the form ) and I can step through all the controls on the form OK in a LOOP., but how do you then paint those constrols onto the bitmap? anyone got any code snippets for capturing a control (e.g button1) on a form (like copyrect type thing) just thinking out loud here with the original code, the copyform code, at the start of this thread, I tried setting the form to be outside the screen area.....but then you end up with black for the part of the form that is outside the screen area (i.e I moved the form to the far right of the screen) so how can you get the form to be repainted off screen and not be black? ---------------------------------------------------------------------- antonio · 2007-09-15 13:42 I have a button, a label and an empty image on a form. The label is copied to the image, at proper position and size, when I press the button. On button event: Code: [Select] var   startx,starty:integer; begin   startx := label1.left  * image1.width  div width;   starty := label1.top * image1.height div height;   image1.canvas.copyrect(rect( startx, starty,          startx+ label1.width*image1.width  div width,          starty+label1.height*image1.height div height                              ), label1.canvas,                                 label1.clientrect); end; ---------------------------------------------------------------------- windy · 2007-09-15 21:00 I have made a bit of progress.....I can now get the edge of the control drawn (i.e a line (box) of where the control is, is drawn!) but not yet the actual surface of the contrl ---------------------------------------------------------------------- antonio · 2007-09-15 21:52 This code maintains aspect ratio, change size of the image in order to become it proportional to the form. ---------------------------------------------------------------------- windy · 2007-09-16 04:03 Hi yup, i saw that the remaining problem now is to add to the DC the controls rect region