6

I need to capture an image of panel.

The problem I am running into is that if the Panel contains a TCombobox the Text does not appear.

procedure AssignPanelImageToPicture(Panel : TPanel;Image : TImage);
var
 B : TBitmap;
begin
 B := TBitmap.Create;
 try
   B.Width := Panel.Width;
   B.Height := Panel.Height;
   B.Canvas.Lock;
   Panel.PaintTo(B.Canvas.Handle,0,0);
   B.Canvas.Unlock;
   Image1.Picture.Assign(B);
  finally
    B.Free;
  end;
end;

Using this code, I drop a panel with a TCombobox on it. Then Enter a value into the Text Property. I also drop a TImage Next two it. Then I add a button to call the above code.

Here is the result:

Imaging of Panel Painting Problem

Is there a better way to capture a true image of the panel.

menjaraz
  • 7,551
  • 4
  • 41
  • 81
Robert Love
  • 12,447
  • 2
  • 48
  • 80
  • ComboBoxes are painted by Windows, and thus your redirection technique (Panel.PaintTo, which tells controls to paint themselves to another location) won't work. You could grab the entire FORM as a Bitmap, but I don't know a workable technique for a single panel. Maybe this helps: http://www.bitwisemag.com/copy/delphi/delphi1.html – Warren P Jan 11 '12 at 23:51
  • TForm.GetFormImage runs into the same problem, it nearly the same code. It does work if I capture the entire screen and then copy out the desired rect but that seems like way too much overhead. The screen capture code is found here: http://code.google.com/p/robstechcorner/source/browse/trunk/Delphi/utils/SCapture.pas – Robert Love Jan 11 '12 at 23:56

1 Answers1

11

What about using the GetDC and BitBlt functions?

procedure AssignPanelImageToPicture(Panel : TPanel;Image : TImage);
var
 B : TBitmap;
 SrcDC: HDC;
begin
 B := TBitmap.Create;
 try
   B.Width := Panel.Width;
   B.Height := Panel.Height;
   SrcDC := GetDC(Panel.Handle);
   try
     BitBlt(B.Canvas.Handle, 0, 0, Panel.ClientWidth, Panel.ClientHeight, SrcDC, 0, 0, SRCCOPY);
   finally
      ReleaseDC(Panel.Handle, SrcDC);
   end;
   Image.Picture.Assign(B);
 finally
    B.Free;
  end;
end;
RRUZ
  • 134,889
  • 20
  • 356
  • 483