Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to print bitmaps and controls placed on a TPanel Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Reporting /Printing
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Tomas Rutkauskas

I have placed several images and assorted graphic controls on a TPanel. Now I want 
to print it. My problem is that the panel does not have a canvas property. Somehow 
I should be able to manipulate the "graphics" on the panel. What I thought might 
work is to do a screen capture of the panel area, but I am not sure what the 
function calls are. Does anybody have any ideas? I want to be able to scale the 
image and print it to a specific part of the page.


The form has a canvas. You can create a new bitmap the same size as your panel and 
then use CopyRect to copy the panel and its content from the form to this in- 
memory bitmap. Then you can print the in-memory bitmap. Here's an example:
2   procedure TFormPrintWindows.ButtonPrintPanelClick(Sender: TObject);
3   var
4     Bitmap: TBitmap;
5     FromLeft, FromTop, PrintedWidth, PrintedHeight: Integer;
6   begin
7     Printer.BeginDoc;
8     try
9       Bitmap := TBitmap.Create;
10      try
11        Bitmap.Width := Panel1.Width;
12        Bitmap.Height := Panel1.Height;
13        Bitmap.PixelFormat := pf24bit; {Avoid palettes}
14        {Copy the panel area from the form into a separate bitmap}
15        Bitmap.Canvas.CopyRect(Rect(0, 0, Bitmap.Width, Bitmap.Height),
16  			FormPrintWindows.Canvas, Rect(Panel1.Left, Panel1.Top, Panel1.Left +
17  			Panel1.Width - 1, Panel1.Top + Panel1.Height - 1));
18        {Assumes 10% left, right and top margin}
19        {Assumes bitmap aspect ratio > ~0.75 for portrait mode}
20        PrintedWidth := MulDiv(Printer.PageWidth, 80, 100); {80%}
21        PrintedHeight := MulDiv(PrintedWidth, Bitmap.Height, Bitmap.Width);
22        FromLeft := MulDiv(Printer.PageWidth, 10, 100); {10%}
23        FromTop := MulDiv(Printer.PageHeight, 10, 100); {10%}
24        PrintBitmap(Printer.Canvas, Rect(FromLeft, FromTop, FromLeft + PrintedWidth,
25          FromTop + PrintedHeight), Bitmap);
26      finally
27        Bitmap.Free
28      end;
29    finally
30      Printer.EndDoc
31    end;
32  end;

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC