How do I copy a form as an image to the clipboard
Asked Answered
P

1

14

I need to copy a form (Delphi 2007) to the clipboard as an image to paste what the user can see into a word document. The clipboard part is not really a problem. The questions is how to get a bitmap for the form.

Searching has turned up multiple options.

All of these options seem to have different problems. Most of the information I am finding seems to be outdated. I can't seem any good source that compares the different options with enough detail for me to make a choice. Any advice on which option to go with.

I have tried these on my form and they all seem to work OK, I just trying to avoid problems down the road. Any advice on what solution to go with?

Update: What Potential Problems with GetFormImage?
Andreas asked what the problem is with GetFormImage. Hopefully nothing anymore, that is part of what I am trying to get an answer to. What has me concerned is so many of my search results seem to be suggesting creative alternatives to using GetFormImage. I was hoping the answers would clear up the waters a little bit.

I would be really happy with an answer that got a lot of up votes that said - GetFormImage used to have some problems but there is no reason not to use it now. :-)

As to the actual problem with GetFormImage. One issue for some users was only the visible part of the form would appear in the image (i.e. you can't capture a hidden or overlapped window). That is not really an issue for me as my entire form is visible.

1) The bigger issues deal with specific support required from the controls on your form. The Delphi 4 Fixes and Known issues page list has this entry (note it is listed as "Deferred to Next"). I could not find a QC entry that showed this resolved:

Area: vcl\core vcl classes

Reference Number: 1088 (Published: 12/16/98)
Status: Deferred to Next
Rel Date Reported: 8/6/98 Severity: Commonly Encountered Type: Basic
Functionality Failure Problem:

The problem is with GetFormImage most nest windows controls like comboboxes, etc. are drawn blank.

2) I am also using the DevExpress controls. At one time their controls (fixed at the end of 2006) did not support the PaintTo messages that GetFormImage was using. This is fixed in the DevExpress release I am using, but it raises other issues with me, what is the chance that other control I am using may not work correctly?

3) Here is a more recent (2010) post on the Embarcadero Groups. The user was having trouble using GetFormImage where part of the graph they were showing on screen did not appear in the final image. They also needed the form caption included (which I do not) and they took the Canvas.CopyRect approach outlined in this post.

4) Here is the quote from the TExcellentImagePrinter page. I would have no problem buying their product if needed. There component looks like it was last updated in 2002 (There is a Delphi 2007 trial version though). I can't tell if I really need to go that direction or not.

You can try using GetFormImage or Form.Print. Try dropping a ComboBox down on a form, then call GetFormImage or Form.Print. If you get a printout, do you see the text in the ComboBox? No? Neither does anyone else! This is only a small example of the problems you will encounter when printing VCL forms.

You can also try using Borland's TI-3155 "A better way to print a form". I wrote the TI when I worked at Borland as a stop gap measure. While it will print the combobox text, it will fail on many printers, it can't print the entire form if your user has resized the form, and it can't print forms that are hidden from view or is located partially off the screen. The code basically produces a screenshot, and to print an image reliably, you would probably want to take a look at our TExcellentImagePrinter product! Why? Simply put, it can require a couple of thousand lines of low level graphics code to get bitmaps to print well under Windows.

Pelota answered 6/7, 2010 at 22:50 Comment(5)
What exactly is the problem with GetFormImage?Champion
I updated the question to include more detail on my concerns with GetFormImage.Pelota
I'm currently fighting the issues of GetFormImage, Canvas.CopyRect (and any BitBlt/StretchBlt variant) and PrintWindow: neither of them are getting the actual stuff drawn on the form. My form is custom-drawn in some areas, and a lot of the time will have another window hovering it, so I get that also included in the capture. If I manage to find a solution, I'll post an answer. But do take notice that in certain scenarios, when your target form is hidden, off screen or obscured by another window, none of the above mentioned solutions will work correctly.Almira
update to my comment: WM_PRINT/WM_PRINTCLIENT/WM_PAINT with hdc as wparam, in XE5 does not work at all. They all get me a white screen. I attempted to implement the stuff on fengyuan, unless I made a mistake somewhere, that also does not work (get's me the empty form. I did test with an empty form and just drawing on it. I did drop a button on the form, and that didn't show up either, so I might have done something wrong. Will come back to this if other solutions also fail)Almira
GetFormImage still does not work properly in Delphi 10.1 Berlin. E.g. TRichEdit components on my form are not capturedOilcloth
C
13

I do not know what the problem is with GetFormImage, but an option that you have not tried (at least not explicitly) is

procedure TForm1.FormClick(Sender: TObject);
var
  bm: TBitmap;
begin

  bm := TBitmap.Create;
  try
    bm.SetSize(ClientWidth, ClientHeight);
    BitBlt(bm.Canvas.Handle, 0, 0, ClientWidth, ClientHeight, Canvas.Handle, 0, 0, SRCCOPY);
    Clipboard.Assign(bm);
  finally
    bm.Free;
  end;

end;

In almost all cases I would expect this to produce the same result as

bm := GetFormImage;
try
  Clipboard.Assign(bm);
finally
  bm.Free;
end;

though. (Also, the Canvas.CopyRect procedure employes StretchBlt which I would expect to produce the same result as BitBlt when no stretching is applied.)

Method 2

You can always use Print Screen:

procedure TForm1.FormClick(Sender: TObject);
begin
  keybd_event(VK_SNAPSHOT, 1, 0, 0);
end;

This will also capture the border and the title bar. If you only wish to obtain the client area, you can crop the image:

procedure TForm1.FormClick(Sender: TObject);
var
  bm, bm2: TBitmap;
  DX, DY: integer;
begin
  Clipboard.Clear;
  keybd_event(VK_SNAPSHOT, 1, 0, 0);
  repeat
    Application.ProcessMessages;
  until Clipboard.HasFormat(CF_BITMAP);
  bm := TBitmap.Create;
  try
    bm.Assign(Clipboard);
    bm2 := TBitmap.Create;
    try
      bm2.SetSize(ClientWidth, ClientHeight);
      DX := (Width - ClientWidth) div 2;
      DY := GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYSIZEFRAME );
      BitBlt(bm2.Canvas.Handle, 0, 0, ClientWidth, ClientHeight, bm.Canvas.Handle, DX, DY, SRCCOPY);
      Clipboard.Assign(bm2);
    finally
      bm2.Free;
    end;
  finally
    bm.Free;
  end;
end;
Champion answered 6/7, 2010 at 23:11 Comment(5)
Andreas, there are some great options there, but it still leaves me with the question of which specific option to use. Since these are all in one answer I can't tell which option is getting the most votes.Pelota
You should choose the simplest method that works for you. If you do not encounter any problems with GetFormImage in your project, then, by all means, use it!Champion
GetFormImage still does not work properly in Delphi 10.1 Berlin. E.g. TRichEdit components on my form are not capturedOilcloth
Only GetFormImage causes memory leak. Assign via bitmap works fine as the bitmap can be freed.Circassia
@user30478: Indeed, that will invariably leak.Champion

© 2022 - 2024 — McMap. All rights reserved.