Recent

Author Topic: Question :convert tpanel.canvas to bitmap when panel out of form bounds?  (Read 6081 times)

Josh

  • Hero Member
  • *****
  • Posts: 1271
Hope someone can help here.

5+ years ago I was playing around with panel canvas, ie copying a panel canvas as a bitmap; that could then be used in another document format.

As many image permutations and resolutions were being done, I thought I had created the panel way off the forms boundary ( out of users view).

Unfortunately I cannot find the code I used back then, but my memory tells me that I just copied the tpanel canvas to a bitmap; when I try this the area of the panel that is off the visible screen is blank.

If I move the panel into the visible area, then the visible part becomes part of the tbitmap.

Has something changed, or is there another way to grab the contents of a tpanel that is outside the screen bounds?

Code: [Select]
var bmp:tbitmap;
begin
  bmp:=tbitmap.Create;
  bmp.Width:=panel1.Width;
  bmp.Height:=panel1.Height;
  panel1.Left:=-panel1.Width+trackbar1.Position;
  bmp.Canvas.CopyRect(rect(0,0,panel1.Width,panel1.Height),panel1.Canvas,rect(0,0,panel1.Width,panel1.Height));
  image1.Picture.Bitmap:=bmp; // temp dump bmp to timage.
  bmp.Free;
end;


Trackbar used to bring panel in/out of focus of screen, image1 is a timage that I drop the copied bmp onto to see what is happening.
« Last Edit: November 20, 2017, 09:43:10 pm by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Question :convert tpanel.canvas to bitmap when panel out of form bounds?
« Reply #1 on: November 20, 2017, 10:36:33 pm »
This function
Code: Pascal  [Select][+][-]
  1. function CreatePanelImage(APanel: TPanel): TBitmap;
  2. begin
  3.   Result := TBitmap.Create;
  4.   try
  5.     Result.SetSize(APanel.ClientWidth, APanel.ClientHeight);
  6.     Result.Canvas.Brush.Color := clWhite;
  7.     Result.Canvas.FillRect(0, 0, Result.Width, Result.Height);
  8.     APanel.PaintTo(Result.Canvas, 0, 0);
  9.   except
  10.     Result.Free;
  11.     raise;
  12.   end;
  13. end;
seems to work even for an off-screen panel (at least on Windows - did not test anything else). See attached demo

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Question :convert tpanel.canvas to bitmap when panel out of form bounds?
« Reply #2 on: November 21, 2017, 12:45:48 am »
Hi WP,

Thanks for your suggestion.

I have created a little test app using the function, but it still shows an issue, if the panel has an image on it, this is not copied to the tbitmap.

It copies the 2 controls I have placed on the form, but not the image.



The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Question :convert tpanel.canvas to bitmap when panel out of form bounds?
« Reply #3 on: November 21, 2017, 12:47:28 am »
Pic, after transfer
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Question :convert tpanel.canvas to bitmap when panel out of form bounds?
« Reply #4 on: November 21, 2017, 02:07:50 am »
Hi

I created a procedure that does similar,
Not sure how cross-platform it is though.
Code: [Select]
procedure GetControlAsBitmap(AControl :TWinControl;ABitmap:TBitmap);
var
  DC: HDC;
begin
  // quick check to make sure control and bitmap are assigned.
  if not Assigned(ABitmap) then Exit;
  if not Assigned(AControl) then Exit;

  DC :=GetWindowDC(AControl.Handle);
  ABitmap.Width  :=AControl.Width;
  ABitmap.Height :=AControl.Height;
  BitBlt(ABitmap.canvas.Handle,0,0,ABitmap.Width,ABitmap.Height,dc,0,0,SrcCopy);
  ReleaseDC(AControl.Handle, DC);
end;             
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Question :convert tpanel.canvas to bitmap when panel out of form bounds?
« Reply #5 on: November 21, 2017, 10:37:17 am »
Hi WP,

Thanks for your suggestion.

I have created a little test app using the function, but it still shows an issue, if the panel has an image on it, this is not copied to the tbitmap.

It copies the 2 controls I have placed on the form, but not the image.
It does if the panel's ParentColor is off.

 

TinyPortal © 2005-2018