Print Control Only

Zath

New member
Joined
Oct 24, 2006
Messages
3
Programming Experience
5-10
I have a form of course, and I need to only print what is inside a panel.

Now, I can do a quick visible=false to all the other controls and so forth, but doesn't look right.

So, how to simply print what is inside a control?

Thanks,

Zath
 
Code like this worked well for me, but the panel have to be fully visible:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
  bmp = getsnap(Panel1)
  PrintDocument1.Print()
  bmp = Nothing
End Sub
 
Private bmp As Bitmap
 
Private Function getsnap(ByVal ctrl As Control) As Bitmap
  Dim bmp As New Bitmap(ctrl.Width, ctrl.Height)
  Dim g As Graphics = Graphics.FromImage(bmp)
  Dim srcPoint As Point = Me.Location
  If Not ctrl.Parent Is Nothing Then
    srcPoint = ctrl.Parent.PointToScreen(ctrl.Location)
  End If
  g.CopyFromScreen(srcPoint, New Point(0, 0), ctrl.Size)
  g.Dispose()
  Return bmp
End Function
 
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles PrintDocument1.PrintPage
  e.Graphics.DrawImage(bmp, e.MarginBounds.Location)
End Sub
 
Thanks for the relply. Worked like I needed.

But, the print preview now needs adjusting.

I have this in it...

Dim ppd As New PrintPreviewDialog()
ppd.Document = pdoc ' for the pdoc_PrintPage
ppd.ShowDialog()

Trying to adjust it so the control shows.

Zath
 
Ok, nevermind.

I adjusted my code above to this:
VB.NET:
[SIZE=2]bmp = getsnap(pnlHolder)[/SIZE]
[SIZE=2]ppd.Document = pdoc[/SIZE]
[SIZE=2]ppd.ShowDialog()[/SIZE]
Thanks again,

Zath
 
Last edited:
Good. I modified the getsnap function a little, but it probably didn't affect your situation. The srcPoint needs to be calculated from its parent controls PointToScreen because control containers can be nested (control within container within container etc and the control location is always relative to its parent). The modification is edited into the code above and works now for any level.
 
Hi John,
This is very nice, but when I copy it into my program, the CopyFromScreen() routine isn't recognized. I imported
System.Drawing.Graphics, but that didn't help. Any idea what I'm missing.

Thanks!!!
Chuck Shultz
 
CopyFromScreen is a new member of Graphics class in .Net 2.0. Get Visual Studio 2005 Express (or better) or use any of the Win32 API screen capture classes found on web.
 
Hi,
It's sinking in with me that the CopyFromScreen utitlity s only going to print that part of my panel that is visible, ad that's not what I want. The other solution I found to this is to print the entire form and it is based on BitBlt. I think I'll go with that other way. Thanks much, though!!!
 
CopyFromScreen and BitBlt is basically the same thing, both need what is copied to be visible, latter can also only copy only part of the screen.
 
Back
Top