Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

  • Call GetFormImage
  • Use the PrintWindow API function that is part of the GDI+
  • Send a WM_PRINT message
  • Copy the Canvas for the current form using Canvas.CopyRect
  • I also found a component called TExcellentFormPrinter that has a solution that claims to works better than any of these options, but I don't know what method it is using.

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.

like image 721
Mark Elder Avatar asked Jul 06 '10 22:07

Mark Elder


1 Answers

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;
like image 184
Andreas Rejbrand Avatar answered Oct 20 '22 02:10

Andreas Rejbrand