In this article I will explain with an example, how to convert (export) DataGridView to Bitmap (PNG) Image file in Windows Forms (WinForms) Application using C# and VB.Net.
Form Design
The Form consists of following controls:
DataGridView – For displaying data.
Button – For converting DataGridView to Bitmap image.
The Button has been assigned with a Click event handler.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Drawing
Binding DataGridView with dummy data in C# and VB.Net
Inside the
Form Load event handler, the
DataGridView is populated with dynamic
DataTable.
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("CustomerId", 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");
dataGridView1.DataSource = dt;
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2){
New DataColumn("CustomerId", 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")
dataGridView1.DataSource = dt
End Sub
Converting DataGridView to Bitmap Image using C# and VB.Net
When the Save as Image Button is clicked, the DataGridView height is set based on the count of rows and height of the RowTemplate of DataGridView.
Then, a Bitmap class object is created where height and width of DataGridView are passed as a parameter to it.
After that, the DataGridView is drawn using DrawToBitmap method and once the DataGridView is drawn, it is resized back to its original height.
Finally, the image file is saved into a Folder (Directory) using Save method of Bitmap class.
C#
private void OnSave(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(dataGridView1.Width, dataGridView1.Height);
dataGridView1.DrawToBitmap(bitmap, new Rectangle(0, 0, dataGridView1.Width, 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 OnSave(sender As Object, e As 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, dataGridView1.Height)
dataGridView1.DrawToBitmap(bitmap, New Rectangle(0, 0, dataGridView1.Width, 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
DataGridView
Exported Bitmap Image
Downloads