Public Sub PrintForm(Optional ByVal fullWindow As Boolean = False) With Me Dim formGraphics As Graphics = .CreateGraphics If fullWindow Then formImage = New Bitmap(.Width, .Height, formGraphics) Else formImage = New Bitmap(.ClientRectangle.Width, _ .ClientRectangle.Height, _ formGraphics) End If Dim memGraphics As Graphics = Graphics.FromImage(formImage) Dim sourceDC As IntPtr = formGraphics.GetHdc Dim targetDC As IntPtr = memGraphics.GetHdc If fullWindow Then Dim widthDelta As Integer = (.Width - _ .ClientRectangle.Width) Dim heightDelta As Integer = (.Height - _ .ClientRectangle.Height) BitBlt(targetDC, _ 0, 0, _ .ClientRectangle.Width + widthDelta, _ .ClientRectangle.Height + heightDelta, _ sourceDC, _ 0 - widthDelta \ 2, 0 - (heightDelta - widthDelta \ 2), _ SRCCOPY) Else BitBlt(targetDC, _ 0, 0, .ClientRectangle.Width, .ClientRectangle.Height, _ sourceDC, _ .ClientRectangle.X, .ClientRectangle.Y, _ SRCCOPY) End If formGraphics.ReleaseHdc(sourceDC) formGraphics.Dispose() memGraphics.ReleaseHdc(targetDC) memGraphics.Dispose() PrintDocument1.Print() formGraphics.Dispose() End With End Sub Private Sub PrintDocument1_PrintPage( _ ByVal sender As System.Object, _ ByVal e As System.Drawing.Printing.PrintPageEventArgs _ ) Handles PrintDocument1.PrintPage e.Graphics.DrawImage(formImage, 100, 200) ' printing position x=100, y=200 End Sub |