How to print bitmaps and controls placed on a TPanel (Views: 30)
Problem/Question/Abstract: 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. Answer: 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: procedure TFormPrintWindows.ButtonPrintPanelClick(Sender: TObject); var Bitmap: TBitmap; FromLeft, FromTop, PrintedWidth, PrintedHeight: Integer; begin Printer.BeginDoc; try Bitmap := TBitmap.Create; try Bitmap.Width := Panel1.Width; Bitmap.Height := Panel1.Height; Bitmap.PixelFormat := pf24bit; {Avoid palettes} {Copy the panel area from the form into a separate bitmap} Bitmap.Canvas.CopyRect(Rect(0, 0, Bitmap.Width, Bitmap.Height), FormPrintWindows.Canvas, Rect(Panel1.Left, Panel1.Top, Panel1.Left + Panel1.Width - 1, Panel1.Top + Panel1.Height - 1)); {Assumes 10% left, right and top margin} {Assumes bitmap aspect ratio > ~0.75 for portrait mode} PrintedWidth := MulDiv(Printer.PageWidth, 80, 100); {80%} PrintedHeight := MulDiv(PrintedWidth, Bitmap.Height, Bitmap.Width); FromLeft := MulDiv(Printer.PageWidth, 10, 100); {10%} FromTop := MulDiv(Printer.PageHeight, 10, 100); {10%} PrintBitmap(Printer.Canvas, Rect(FromLeft, FromTop, FromLeft + PrintedWidth, FromTop + PrintedHeight), Bitmap); finally Bitmap.Free end; finally Printer.EndDoc end; end; |