Module PrintObject
Dim screenshot As Bitmap
Public Sub PrintControl(ByRef sender As Control)
GetScreenshotOfControl(sender)
PrepareAndPrint()
End Sub
Public Sub PrintPreviewControl(ByRef sender As Control)
GetScreenshotOfControl(sender)
PrepareAndPrintPreview()
End Sub
Public Sub PrintForm(ByRef myform As Windows.Forms.Form)
GetScreenShotOfForm(myform)
PrepareAndPrint()
End Sub
Public Sub PrintPreviewForm(ByRef myform As Windows.Forms.Form)
GetScreenShotOfForm(myform)
PrepareAndPrintPreview()
End Sub
Public Sub PrintFormClientArea(ByRef myform As Windows.Forms.Form)
GetScreenShotOfFormClientArea(myform)
PrepareAndPrint()
End Sub
Public Sub PrintPreviewFormClientArea(ByRef myform As Windows.Forms.Form)
GetScreenShotOfFormClientArea(myform)
PrepareAndPrintPreview()
End Sub
' Remaining routines are helper routines to perform tasks assigned by public routines
Private Sub GetScreenshotOfControl(ByRef sender As Control)
' First get the local coordinates of the control
Dim mylocalsize As Rectangle = New Rectangle(sender.Location, sender.Size)
' Now get the global coordinates of the control
Dim mysize As Rectangle = sender.RectangleToScreen(mylocalsize)
screenshot = New Bitmap(mysize.Width, mysize.Height)
Dim g As Graphics = Graphics.FromImage(screenshot)
Dim pt As Point = New Point(0, 0)
' myloc is the top-left of the client area of the form in global co-ordinates
Dim myloc As Point = New Point(mysize.Location.X, mysize.Location.Y)
g.CopyFromScreen(myloc, pt, mysize.Size)
End Sub
Private Sub GetScreenshotOfForm(ByRef myform As Windows.Forms.Form)
Dim mysize As Rectangle = New Rectangle(myform.Location, myform.Size)
screenshot = New Bitmap(mysize.Width, mysize.Height)
Dim g As Graphics = Graphics.FromImage(screenshot)
Dim pt As Point = New Point(0, 0)
' myloc is the top-left of the client area of the form in global co-ordinates
Dim myloc As Point = New Point(mysize.Location.X, mysize.Location.Y)
g.CopyFromScreen(myloc, pt, mysize.Size)
End Sub
Private Sub GetScreenshotOfFormClientArea(ByRef myform As Windows.Forms.Form)
Dim mysize As Rectangle = myform.ClientRectangle
screenshot = New Bitmap(mysize.Width, mysize.Height)
Dim g As Graphics = Graphics.FromImage(screenshot)
Dim pt As Point = New Point(0, 0)
' myloc is the top-left of the client area of the form in global co-ordinates
Dim myloc As Point = New Point(mysize.Location.X + myform.Location.X, _
mysize.Location.Y + myform.Location.Y + (myform.Height - mysize.Height))
g.CopyFromScreen(myloc, pt, mysize.Size)
End Sub
Private Sub PrepareAndPrint()
' This is a standard print function with a save dialog
' added if the user chooses to print to a file
Dim doc As Printing.PrintDocument = New Printing.PrintDocument
Dim printer As PrintDialog = New PrintDialog
AddHandler doc.PrintPage, AddressOf PrintPageHandler
printer.Document = doc
Dim response As Windows.Forms.DialogResult = printer.ShowDialog()
If response = Windows.Forms.DialogResult.OK Then
If doc.PrinterSettings.PrintToFile = True Then
' Allow the user to set the filename using a dialog
Dim save As New SaveFileDialog()
save.Filter = "Plain Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
save.CheckPathExists = True
save.Title = "Spool printer output"
If save.ShowDialog() = Windows.Forms.DialogResult.OK Then
doc.PrinterSettings.PrintFileName = save.FileName
Else
' User must not want to print if they cancelled
Exit Sub
End If
End If
doc.Print()
End If
End Sub
Private Sub PrepareAndPrintPreview()
Dim doc As Printing.PrintDocument = New Printing.PrintDocument
Dim printer As PrintPreviewDialog = New PrintPreviewDialog
AddHandler doc.PrintPage, AddressOf PrintPageHandler
printer.Document = doc
printer.ShowDialog()
End Sub
Private Sub PrintPageHandler(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim canvas As Graphics = e.Graphics
' The printer will print the image to whatever bounds are set here (in the next two lines)
Dim topLeft As Point = New Point(0, 0)
Dim bottomRight As Point = New Point(e.PageBounds.Width, e.PageBounds.Height)
' The rest of the code should not need to be modified
' This algorithm makes sure the image scales properly
Dim pageHeight As Integer = bottomRight.Y - topLeft.Y
Dim pageWidth As Integer = bottomRight.X - topLeft.X
Dim scaleHeight As Single = pageHeight / screenshot.Height
Dim scaleWidth As Single = pageWidth / screenshot.Width
' NewHeight and NewWidth determine the drawing area.
' Assume scaleHeight < scaleWidth
Dim newHeight As Integer = scaleHeight * screenshot.Height
Dim newWidth As Integer = scaleHeight * screenshot.Width
' Now check assumption, and correct if wrong
If scaleWidth < scaleHeight Then
newHeight = scaleWidth * screenshot.Height
newWidth = scaleWidth * screenshot.Width
End If
canvas.DrawImage(screenshot, topLeft.X, topLeft.Y, newWidth, newHeight)
End Sub
End Module