In this article I will explain how to print the contents of Form in Windows Forms (WinForms) Application using C# and VB.Net.
The Form contents will be printed without the Form Header and Form borders.
 
Form Controls
In the below Form, there’s a DataGridView, a Print Button, a PrintPreviewDialog and a PrintDocument controls.
Print contents of Form in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Populating DataGridView
In order to populate the DataGridView, I have created a dynamic DataTable with some sample data.
C#
private void Form1_Load(object sender, EventArgs e)
{
    this.BindDataGridView();
}
 
private void BindDataGridView()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                                new DataColumn("Name", typeof(string)),
                                new DataColumn("Country",typeof(string)) });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    this.dataGridView1.DataSource = dt;
}
 
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    BindDataGridView()
End Sub
 
Private Sub BindDataGridView()
    Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    Me.dataGridView1.DataSource = dt
End Sub
 
Print contents of Form in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Printing the contents of Form
When the Print Button is clicked, first a Panel control is added to the Form. Then an object of the Graphic class is created from the Panel and its Height and Width is set to that of the Height and Width of the Form after excluding its Header and Borders.
Using the Graphics object, Bitmap object is created from the Panel and the Form area covered by the Panel is captured within it.
Finally the PrintPreviewDialog is shown and the Bitmap object is printed.
C#
Bitmap bitmap;
private void btnPrint_Click(object sender, EventArgs e)
{
    //Add a Panel control.
    Panel panel = new Panel();
    this.Controls.Add(panel);
 
    //Create a Bitmap of size same as that of the Form.
    Graphics grp = panel.CreateGraphics();
    Size formSize = this.ClientSize;
    bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
    grp = Graphics.FromImage(bitmap);
 
    //Copy screen area that that the Panel covers.
    Point panelLocation = PointToScreen(panel.Location);
    grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
 
    //Show the Print Preview Dialog.
    printPreviewDialog1.Document = printDocument1;
    printPreviewDialog1.PrintPreviewControl.Zoom = 1;
    printPreviewDialog1.ShowDialog();
}
 
private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    //Print the contents.
    e.Graphics.DrawImage(bitmap, 0, 0);
}
 
VB.Net
Private bitmap As Bitmap
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    'Add a Panel control.
    Dim panel As New Panel()
    Me.Controls.Add(panel)
 
    'Create a Bitmap of size same as that of the Form.
    Dim grp As Graphics = panel.CreateGraphics()
    Dim formSize As Size = Me.ClientSize
    bitmap = New Bitmap(formSize.Width, formSize.Height, grp)
    grp = Graphics.FromImage(bitmap)
 
    'Copy screen area that that the Panel covers.
    Dim panelLocation As Point = PointToScreen(panel.Location)
    grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize)
 
    'Show the Print Preview Dialog.
    printPreviewDialog1.Document = printDocument1
    printPreviewDialog1.PrintPreviewControl.Zoom = 1
    printPreviewDialog1.ShowDialog()
End Sub
 
Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles printDocument1.PrintPage
    'Print the contents.
    e.Graphics.DrawImage(bitmap, 0, 0)
End Sub
 
 
Screenshots
Print Preview Dialog displaying the contents of the Form
Print contents of Form in Windows Forms (WinForms) Application using C# and VB.Net
 
Printed contents of Form
Print contents of Form in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Downloads

Print_Form.zip