Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Printing in VB.net

Posted on 2009-04-12
6
Medium Priority
?
2,194 Views
Last Modified: 2013-12-26
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
Comment
Question by:joshbennett04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24128254
What version VB.Net?
0
 
LVL 15

Accepted Solution

by:
oobayly earned 2000 total points
ID: 24129157
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
 
LVL 1

Author Comment

by:joshbennett04
ID: 24134839
That looks pretty promising, I will give it a shot tomorrow
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 1

Author Comment

by:joshbennett04
ID: 24134842
vs2008 btw
0
 
LVL 1

Author Comment

by:joshbennett04
ID: 24185797
Feel pretty stupid now that I know how to do that.. Worked perfectly.

Thanks!
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24185839
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question