' API call to help generate final screenshot
Private Declare Auto Function BitBlt Lib "gdi32.dll" (ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As System.Int32) As Boolean
' Variable to store screenshot
Private bmpScreenshot As Bitmap
Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click, mnuFilePreview.Click
GrabScreen()
Try
PrintPreviewDialog1.Document = Me.PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch es As Exception
MessageBox.Show(es.Message)
End Try
End Sub
Private Sub GrabScreen()
' Performs a screenshot, saving results to bmpScreenshot
Dim objGraphics As Graphics = Me.CreateGraphics
Dim objSize As Size = Me.Size
Const SRCCOPY As Integer = &HCC0020
bmpScreenshot = New Bitmap(objSize.Width, objSize.Height, _
objGraphics)
Dim objGraphics2 As Graphics = _
objGraphics.FromImage(bmpScreenshot)
Dim deviceContext1 As IntPtr = objGraphics.GetHdc
Dim deviceContext2 As IntPtr = objGraphics2.GetHdc
BitBlt(deviceContext2, 0, 0, Me.ClientRectangle.Width, _
Me.ClientRectangle.Height, deviceContext1, 0, 0, SRCCOPY)
objGraphics.ReleaseHdc(deviceContext1)
objGraphics2.ReleaseHdc(deviceContext2)
End Sub
Private Sub MyPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
' Method that handles the printing
Dim objImageToPrint As Graphics = e.Graphics
objImageToPrint.DrawImage(bmpScreenshot, 0, 0)
e.HasMorePages = False
End Sub