Imprimer un form VB.net

medamineboulbhaiem Messages postés 24 Date d'inscription mardi 3 avril 2012 Statut Membre Dernière intervention 28 juin 2012 - 14 juin 2012 à 09:57
medamineboulbhaiem Messages postés 24 Date d'inscription mardi 3 avril 2012 Statut Membre Dernière intervention 28 juin 2012 - 18 juin 2012 à 09:48
Bonjour
je veux imprimer des forms dans mon application sous vb.net
j'ai utilisé 'PrintForm' le problème c'est qu'il ne m'imprime pas tout le form il capture juste la moitié, je crois qu'il le prend en mode portrait et moi je veu qu'il le capture en mode paysage
est ce quelqu’un a déjà eu le même problème ? j'ai cherché partout sans trouver la solution
A voir également:

7 réponses

cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
15 juin 2012 à 08:55
Salut

une class qui permet d'imprimer la form sans
la bordure ou d'imprimer un control de la form

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


pour utiliser

Dim printer As New ClassImpression(me)
 printer.printdoc()


si tu veux seulement imprimer par ex le picturebox
Dim printer As New ClassImpression(picturebox1)
printer.printdoc()
2
Rejoignez-nous