• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2196
  • Last Modified:

Printing in VB.net

I have a form with a PictureBox in it.  The PictureBox has controls paced over it.  The PictureBox is the parent to the controls.  The PictureBox is larger than the form and I use a ScrollBar to scroll down to view the entire PictureBox.  I can use the following function and print the PictureBox, but not the controls on it.  Since the PictureBox is larger than my screen, I cannot do a print screen.  The idea was to take the document and make the form look exactly the same as the document, so I just created controls in the positions over the picture. I do not have to use a PictureBox, as I just used it to get the controls placed in the correct positions.  Any help is appreciated!!
PrintPreviewDialog1.Document = PrintDocument2
        PrintPreviewDialog1.ShowDialog()
 
 
    Private Sub PrintDocument2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
        e.PageSettings.Color = False
        e.Graphics.DrawImage(PictureBox2.Image, e.PageBounds.X, e.PageBounds.Y, 897, 1114)
    End Sub

Open in new window

Screenshot.png
0
joshbennett04
Asked:
joshbennett04
  • 3
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version VB.Net?
0
 
oobaylyCommented:
If you host all your controls in a ScrollableControl, ie a Panel, you can then call this method to render the control. Make sure the container control is big enough to display the largest child control, though in your case this shouldn't be an issue.

The screenshot shows a pane (top control) containg several child controls, and a picturebox displaying the rendered panel.
Private Function DrawControl(ByVal container As ScrollableControl) As Bitmap
    ' Create a bitmap with the display bounds of the container
    Dim img As New Bitmap(container.DisplayRectangle.Width, container.DisplayRectangle.Height)
    Dim g As Graphics = Graphics.FromImage(img)
    
    ' Draw each control onto the bitmap
    For Each child As Control In container.Controls
        ' Scroll the child control into view
        container.ScrollControlIntoView(child)
        
        ' Draw the child control onto a temporary bitmap
        Dim tmp As New Bitmap(child.Width, child.Height)
        child.DrawToBitmap(tmp, New Rectangle(0, 0, child.Width, child.Height))
        
        ' Draw the temporary bitmap to the main bitmap, taking into account
        ' that the container has been scrolled
        g.DrawImage(tmp, child.Left - container.AutoScrollPosition.X, child.Top - container.AutoScrollPosition.Y)
        
        tmp.Dispose()
    Next
    g.Flush()
    g.Dispose()
    
    Return img
End Function

Open in new window

example.png
0
 
joshbennett04Author Commented:
That looks pretty promising, I will give it a shot tomorrow
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
joshbennett04Author Commented:
vs2008 btw
0
 
joshbennett04Author Commented:
Feel pretty stupid now that I know how to do that.. Worked perfectly.

Thanks!
0
 
oobaylyCommented:
To be fair, I went through several iterations getting the correct canvas area, and making sure the controls were rendered in the correct position, so I would abuse yourself too much :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now