Please Help me to print a form using vb.net2008 code.
Please send me in details.
Help me..............

7 Years
Discussion Span
Last Post by aashiq

Try the following code.

Create the a module 'Module1' and use the following code.

Module Module1
    Private Print_Image As Image
    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 WithEvents PrintDocument1 As New Printing.PrintDocument()

    Public Sub doPrint(ByVal frm As Form)
        'We make the form look pritty befor its picture  
        'Get a Graphics Object from the form 
        Dim FormG As Graphics = frm.CreateGraphics
        'Create a bitmap from that graphics 
        Dim i As New Bitmap(frm.Width, frm.Height, FormG)
        'Create a Graphics object in memory from that bitmap 
        Dim memG As Graphics = Graphics.FromImage(i)
        'get the IntPtr's of the graphics 
        Dim HDC1 As IntPtr = FormG.GetHdc
        Dim HDC2 As IntPtr = memG.GetHdc
        'get the picture 
        BitBlt(HDC2, 0, 0, frm.ClientRectangle.Width, _
        frm.ClientRectangle.Height, HDC1, 0, 0, 13369376)
        'Clone the bitmap so we can dispose this one 
        Print_Image = i.Clone()
        'Clean Up 
        'Show the PrintDialog 
        Dim PrintDialog1 As PrintDialog = New PrintDialog()

        PrintDialog1.Document = PrintDocument1
        Dim r As DialogResult = PrintDialog1.ShowDialog
        If r = DialogResult.OK Then
            'Print the document 
        End If
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim formGraphics As Graphics = e.Graphics
        e.Graphics.DrawImage(Print_Image, New System.Drawing.PointF(0, 0))
        e.HasMorePages = False
    End Sub

End Module

Create a form in VB.NET and use the following code.

Public Class Form5
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.CheckBox1 = New System.Windows.Forms.CheckBox
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox1.Location = New System.Drawing.Point(24, 168)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "TextBox1"
        Me.CheckBox1.Location = New System.Drawing.Point(160, 40)
        Me.CheckBox1.Name = "CheckBox1"
        Me.CheckBox1.TabIndex = 1
        Me.CheckBox1.Text = "CheckBox1"
        Me.GroupBox1.Location = New System.Drawing.Point(24, 224)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.TabIndex = 2
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "GroupBox1"
        Me.ListBox1.Location = New System.Drawing.Point(24, 40)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(120, 95)
        Me.ListBox1.TabIndex = 3
        Me.Button1.Location = New System.Drawing.Point(208, 96)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 4
        Me.Button1.Text = "Print"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 357)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.ListBox1, Me.GroupBox1, Me.CheckBox1, Me.TextBox1})
        Me.Name = "Form5"
        Me.Text = "Form5"

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    End Sub
End Class

Reference: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_21074886.html

Edited by Ramesh S: n/a


Ya i used it but it make a snap shot to print the gridview. I wanna get gradually print from a gridview. Please help me. Thanks for your suggestion sir.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.