Results 1 to 1 of 1

Thread: [vb2005] Print/Preview a form or a control

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    [vb2005] Print/Preview a form or a control

    Preamble:
    I needed to print a form with a mass of different controls on it, one in particular was a picturebox which had a complex graphic drawn on it. I looked for help on printing, and though I found a fair amount, I couldn't solve my problem. Through a combination of perservence, searching, and the reading of many many Intellisense prototypes, I found a way to print from a screenshot. This is not a perfect solution, but if you have a form or control that you need printed and an easy way to do it, I have written something you may be able to use.

    Feel free to post any problems, corrections, or comments

    Limitations of this code:
    As this code takes a screenshot, the object you are printing (eg the form you want to print) must be on top of all others. Otherwise, you will end up with a screenshot of something you didn't intend.

    Details:
    Note: Currently the control printing and previewing is not working. I will try and fix this later, but just ignore it for now (or fix it if you want)

    Below is a module that can be added to any vb.net 2005 project to allow for easy printing (and print previews) of three things: A form, a form's client area, and a control. There are six (6) public routines at your disposal to print what you need quickly. Also, the code should help anybody who ran into the problem of not knowing how to print or where to begin searching.

    I will provide a list of information sources I used to write this after the module:

    VB Code:
    1. Module PrintObject
    2.  
    3.     Dim screenshot As Bitmap
    4.  
    5.     Public Sub PrintControl(ByRef sender As Control)
    6.         GetScreenshotOfControl(sender)
    7.         PrepareAndPrint()
    8.     End Sub
    9.  
    10.     Public Sub PrintPreviewControl(ByRef sender As Control)
    11.         GetScreenshotOfControl(sender)
    12.         PrepareAndPrintPreview()
    13.     End Sub
    14.  
    15.     Public Sub PrintForm(ByRef myform As Windows.Forms.Form)
    16.         GetScreenShotOfForm(myform)
    17.         PrepareAndPrint()
    18.     End Sub
    19.  
    20.     Public Sub PrintPreviewForm(ByRef myform As Windows.Forms.Form)
    21.         GetScreenShotOfForm(myform)
    22.         PrepareAndPrintPreview()
    23.     End Sub
    24.  
    25.     Public Sub PrintFormClientArea(ByRef myform As Windows.Forms.Form)
    26.         GetScreenShotOfFormClientArea(myform)
    27.         PrepareAndPrint()
    28.     End Sub
    29.  
    30.     Public Sub PrintPreviewFormClientArea(ByRef myform As Windows.Forms.Form)
    31.         GetScreenShotOfFormClientArea(myform)
    32.         PrepareAndPrintPreview()
    33.     End Sub
    34.  
    35.  
    36.     ' Remaining routines are helper routines to perform tasks assigned by public routines
    37.  
    38.  
    39.     Private Sub GetScreenshotOfControl(ByRef sender As Control)
    40.         ' First get the local coordinates of the control
    41.         Dim mylocalsize As Rectangle = New Rectangle(sender.Location, sender.Size)
    42.         ' Now get the global coordinates of the control
    43.         Dim mysize As Rectangle = sender.RectangleToScreen(mylocalsize)
    44.         screenshot = New Bitmap(mysize.Width, mysize.Height)
    45.         Dim g As Graphics = Graphics.FromImage(screenshot)
    46.         Dim pt As Point = New Point(0, 0)
    47.         ' myloc is the top-left of the client area of the form in global co-ordinates
    48.         Dim myloc As Point = New Point(mysize.Location.X, mysize.Location.Y)
    49.         g.CopyFromScreen(myloc, pt, mysize.Size)
    50.     End Sub
    51.  
    52.     Private Sub GetScreenshotOfForm(ByRef myform As Windows.Forms.Form)
    53.         Dim mysize As Rectangle = New Rectangle(myform.Location, myform.Size)
    54.         screenshot = New Bitmap(mysize.Width, mysize.Height)
    55.         Dim g As Graphics = Graphics.FromImage(screenshot)
    56.         Dim pt As Point = New Point(0, 0)
    57.         ' myloc is the top-left of the client area of the form in global co-ordinates
    58.         Dim myloc As Point = New Point(mysize.Location.X, mysize.Location.Y)
    59.         g.CopyFromScreen(myloc, pt, mysize.Size)
    60.     End Sub
    61.  
    62.     Private Sub GetScreenshotOfFormClientArea(ByRef myform As Windows.Forms.Form)
    63.         Dim mysize As Rectangle = myform.ClientRectangle
    64.         screenshot = New Bitmap(mysize.Width, mysize.Height)
    65.         Dim g As Graphics = Graphics.FromImage(screenshot)
    66.         Dim pt As Point = New Point(0, 0)
    67.         ' myloc is the top-left of the client area of the form in global co-ordinates
    68.         Dim myloc As Point = New Point(mysize.Location.X + myform.Location.X, _
    69.         mysize.Location.Y + myform.Location.Y + (myform.Height - mysize.Height))
    70.         g.CopyFromScreen(myloc, pt, mysize.Size)
    71.     End Sub
    72.  
    73.     Private Sub PrepareAndPrint()
    74.         ' This is a standard print function with a save dialog
    75.         '   added if the user chooses to print to a file
    76.         Dim doc As Printing.PrintDocument = New Printing.PrintDocument
    77.         Dim printer As PrintDialog = New PrintDialog
    78.         AddHandler doc.PrintPage, AddressOf PrintPageHandler
    79.         printer.Document = doc
    80.         Dim response As Windows.Forms.DialogResult = printer.ShowDialog()
    81.         If response = Windows.Forms.DialogResult.OK Then
    82.             If doc.PrinterSettings.PrintToFile = True Then
    83.                 ' Allow the user to set the filename using a dialog
    84.                 Dim save As New SaveFileDialog()
    85.                 save.Filter = "Plain Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
    86.                 save.CheckPathExists = True
    87.                 save.Title = "Spool printer output"
    88.                 If save.ShowDialog() = Windows.Forms.DialogResult.OK Then
    89.                     doc.PrinterSettings.PrintFileName = save.FileName
    90.                 Else
    91.                     ' User must not want to print if they cancelled
    92.                     Exit Sub
    93.                 End If
    94.             End If
    95.             doc.Print()
    96.         End If
    97.     End Sub
    98.  
    99.     Private Sub PrepareAndPrintPreview()
    100.         Dim doc As Printing.PrintDocument = New Printing.PrintDocument
    101.         Dim printer As PrintPreviewDialog = New PrintPreviewDialog
    102.         AddHandler doc.PrintPage, AddressOf PrintPageHandler
    103.         printer.Document = doc
    104.         printer.ShowDialog()
    105.     End Sub
    106.  
    107.     Private Sub PrintPageHandler(ByVal sender As Object, _
    108.     ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    109.         Dim canvas As Graphics = e.Graphics
    110.         ' The printer will print the image to whatever bounds are set here (in the next two lines)
    111.         Dim topLeft As Point = New Point(0, 0)
    112.         Dim bottomRight As Point = New Point(e.PageBounds.Width, e.PageBounds.Height)
    113.         ' The rest of the code should not need to be modified
    114.         ' This algorithm makes sure the image scales properly
    115.         Dim pageHeight As Integer = bottomRight.Y - topLeft.Y
    116.         Dim pageWidth As Integer = bottomRight.X - topLeft.X
    117.         Dim scaleHeight As Single = pageHeight / screenshot.Height
    118.         Dim scaleWidth As Single = pageWidth / screenshot.Width
    119.         ' NewHeight and NewWidth determine the drawing area.
    120.         ' Assume scaleHeight < scaleWidth
    121.         Dim newHeight As Integer = scaleHeight * screenshot.Height
    122.         Dim newWidth As Integer = scaleHeight * screenshot.Width
    123.         ' Now check assumption, and correct if wrong
    124.         If scaleWidth < scaleHeight Then
    125.             newHeight = scaleWidth * screenshot.Height
    126.             newWidth = scaleWidth * screenshot.Width
    127.         End If
    128.         canvas.DrawImage(screenshot, topLeft.X, topLeft.Y, newWidth, newHeight)
    129.     End Sub
    130.  
    131. End Module

    Resources:
    Article on printing in vb.net 2003 (My main resource)
    Another such module for printing (Part of the inspiration for this)
    Some printing fundamentals from jmcilhinney
    Another codeback submission that helps with printing text documents (I didn't use it, but it might be helpful)
    Last edited by ZaNi; Sep 7th, 2006 at 01:46 AM.
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width