Imports System
Imports System.Drawing.Printing
Public Class Form1
<System.Runtime.InteropServices.DllImport("gdi32.dll")> _
Public Shared Function BitBlt(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 Integer) As Boolean
End Function
<System.Runtime.InteropServices.DllImport("User32.dll")> _
Public Shared Function GetWindowDC(ByVal hwd As IntPtr) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("User32.dll")> _
Public Shared Function ReleaseDC(ByVal hwd As IntPtr, ByVal dc As IntPtr) As Integer
End Function
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Public Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
End Function
<Serializable(), System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
Private memoryImage As Bitmap
Private Sub CaptureScreen()
Dim mygraphics As Graphics = Me.CreateGraphics()
Dim r As RECT
GetWindowRect(Me.Handle, r)
Dim s As Rectangle = Rectangle.FromLTRB(r.Left, r.Top, r.Right, r.Bottom)
memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
Dim dc1 As IntPtr = GetWindowDC(Me.Handle)
Dim dc2 As IntPtr = memoryGraphics.GetHdc()
BitBlt(dc2, 0, 0, s.Width, s.Height, dc1, 0, 0, 13369376)
ReleaseDC(Me.Handle, dc1)
memoryGraphics.ReleaseHdc(dc2)
End Sub
Private Sub Document_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
e.Graphics.DrawImage(memoryImage, 0, 0)
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CaptureScreen()
PrintDialog1.ShowDialog()
Dim pd As PrintDocument = New PrintDocument
pd.PrinterSettings = PrintDialog1.PrinterSettings
AddHandler pd.PrintPage, AddressOf Document_PrintPage
pd.Print()
End Sub