控件 内容 打印

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

                Dim PrintDialog As New PrintDialog
                Dim dlg As New PrintPreviewDialog

                Dim graphicBS As Graphics
                graphicBS = pnlLCC.CreateGraphics()

                Dim size As Size
                size = pnlLCC.Size

                'Dim img As Bitmap
                img = New Bitmap(size.Width, size.Height, graphicBS)

                Dim graphicImg As Graphics
                graphicImg = Graphics.FromImage(img)

                Dim dc1, dc2 As IntPtr
                dc1 = graphicBS.GetHdc()
                dc2 = graphicImg.GetHdc()

                NativeMethods.BitBlt(dc2, 0, 0, pnlLCC.ClientSize.Width, pnlLCC.ClientSize.Height, dc1, 0, 0, NativeMethods.SRCCOPY)


                printImg = CType(img.Clone(), Image)
                graphicBS.ReleaseHdc(dc1)
                graphicImg.ReleaseHdc(dc2)
                graphicBS.Dispose()
                graphicImg.Dispose()
                img.Dispose()

                printImg.Save("C:BS.jpg")

                AddHandler printDoc.PrintPage, AddressOf PrintDocument_PrintPage

                PrintDialog.Document = printDoc
                PrintDialog.UseEXDialog = True
                Dim result As DialogResult = PrintDialog.ShowDialog()
                If (result = DialogResult.OK) Then
                    printDoc.Print()
                End If

    End Sub

    Private Sub PrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

	'!!!!!!!!!!!!!!!!!!!!!!! 重点
        Dim width As Integer = printDoc.DefaultPageSettings.Bounds.Width
        Dim height As Integer = printDoc.DefaultPageSettings.Bounds.Height
        e.Graphics.DrawImage(printImg, 0, 0, width, height)

    End Sub
Public Class NativeMethods

    Public Const SRCCOPY As UInteger = &HCC0020

    <System.Runtime.InteropServices.DllImport("user32.dll ")> _
    Public Shared Function GetDesktopWindow() As IntPtr

    End Function


    <System.Runtime.InteropServices.DllImport("user32.dll ")> _
            Public Shared Function GetWindowDC(ByVal hwnd As IntPtr) As IntPtr

    End Function


    <System.Runtime.InteropServices.DllImport("gdi32.dll ")> _
            Public Shared Function BitBlt(ByVal hDestDC As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As IntPtr, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As System.Int32) As UInt64

    End Function


End Class
 

你可能感兴趣的:(打印)