1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| Imports System.Drawing.Printing
Public Class ClassImpression
Private myimage As Bitmap
Private thectrl As Control
Private Function Capturectrl(ByVal ctrl As Control) As Bitmap
Dim memoryImage As Bitmap
Dim memoryGraphics As Graphics
memoryImage = Nothing
Dim mygraphics As Graphics = ctrl.CreateGraphics()
Dim s As Size = ctrl.Size
If TypeOf ctrl Is Form AndAlso DirectCast(ctrl, Form).FormBorderStyle <> FormBorderStyle.None Then
memoryImage = New Bitmap(s.Width - 10, s.Height - (SystemInformation.FrameBorderSize.Width + 6 + SystemInformation.CaptionHeight), mygraphics)
memoryGraphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(0, (SystemInformation.FrameBorderSize.Width + 0 + SystemInformation.CaptionHeight), 0, 0, New Size(memoryImage.Width, memoryImage.Height), CopyPixelOperation.SourceCopy)
Else
memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
memoryGraphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(ctrl.Left, ctrl.Top, 0, 0, New Size(memoryImage.Width, memoryImage.Height), CopyPixelOperation.SourceCopy)
End If
Return memoryImage
End Function
Public Function GetPreview() As Bitmap
Return Capturectrl(thectrl)
End Function
Public Sub printdoc()
Try
Dim prtd As New PrintDialog
'document à imprimer
Dim doc As New PrintDocument
myimage = Capturectrl(thectrl)
AddHandler doc.PrintPage, AddressOf printform
'paramètres de page
Dim ps As New PageSettings
ps.Landscape = False
doc.DefaultPageSettings = ps
'indique à la prévisualisation le document à montrer
prtd.Document = doc
If prtd.ShowDialog = DialogResult.OK Then
prtd.Document.Print()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub printform(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Try
e.Graphics.DrawImage(myimage, 0, 0)
e.HasMorePages = False
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub New(ByVal ctrl As Control)
thectrl = ctrl
End Sub
End Class |
Partager