Dim memoryImage
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 Int32) As Boolean
Private Sub btnPrintpreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintpreview.Click
CaptureScreen()
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
ในฟอร์มให้เพิ่มคอนโทล PrintDocument1 กับ PrintPreviewDialog1 ด้วย
อันนี้ Sub Code (VB.NET)
Private Sub CaptureScreen()
Dim mygraphics As Graphics = Panel3.CreateGraphics()
Dim s As Size = Panel3.Size
memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
Dim dc1 As IntPtr = mygraphics.GetHdc
Dim dc2 As IntPtr = memoryGraphics.GetHdc
BitBlt(dc2, 0, 0, Panel3.ClientRectangle.Width, Panel3.ClientRectangle.Height, dc1, 0, 0, 13369376)
mygraphics.ReleaseHdc(dc1)
memoryGraphics.ReleaseHdc(dc2)
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawImage(memoryImage, 10, 10)
End Sub