In this article I will explain how to convert (export) DataGridView to Bitmap (PNG) Image file in Windows Forms (WinForms) Application using C# and VB.Net.
 
 
Form Controls
In the below Form, there’s a DataGridView and a Save as Image Button.
Convert (Export) DataGridView to Bitmap (PNG) Image 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
 
Convert (Export) DataGridView to Bitmap (PNG) Image in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Convert (Export) DataGridView to Bitmap (PNG) Image
When the Save as Image Button is clicked, first the DataGridView height is adjusted based on the count of rows so that all rows of the DataGridView are captured in the Image.
A Bitmap object is created and the DataGridView is drawn in to the Bitmap object.
Once the DataGridView is drawn, the DataGridView is resized back to its original height.
Finally the Bitmap object is saved to a folder as PNG image file.
C#
private void btnSave_Click(object sender, EventArgs e)
{
    //Resize DataGridView to full height.
    int height = dataGridView1.Height;
    dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height;
 
    //Create a Bitmap and draw the DataGridView on it.
    Bitmap bitmap = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
    dataGridView1.DrawToBitmap(bitmap, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
 
    //Resize DataGridView back to original height.
    dataGridView1.Height = height;
 
    //Save the Bitmap to folder.
    bitmap.Save(@"D:\Images\DataGridView.png");
}
 
VB.Net
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
    'Resize DataGridView to full height.
    Dim height As Integer = dataGridView1.Height
    dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height
 
    'Create a Bitmap and draw the DataGridView on it.
    Dim bitmap As Bitmap = New Bitmap(Me.dataGridView1.Width, Me.dataGridView1.Height)
    dataGridView1.DrawToBitmap(bitmap, New Rectangle(0, 0, Me.dataGridView1.Width, Me.dataGridView1.Height))
 
    'Resize DataGridView back to original height.
    dataGridView1.Height = height
 
    'Save the Bitmap to folder.
    bitmap.Save("D:\Images\DataGridView.png")
End Sub
 
 
Screenshot
Convert (Export) DataGridView to Bitmap (PNG) Image in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Downloads