Parametre olarak girilen DataGridView nesnesini Bitmap\\\'a cevirir.
Public Function DGV2BMP(ByRef DGV As DataGridView) As Bitmap
Dim i, j, startX, startY, colHS, rowHS As Integer
Dim picBox As New PictureBox
Dim lr As RectangleF
colHS = DGV.ColumnHeadersHeight
rowHS = DGV.RowHeadersWidth
picBox.Width = DGV.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + rowHS
picBox.Height = DGV.Rows.GetRowsHeight(DataGridViewElementStates.Visible) + colHS
Dim bmp As New Bitmap(picBox.Width, picBox.Height)
picBox.Image = bmp
Dim g As Graphics = Graphics.FromImage(picBox.Image)
g.Clear(Color.White)
g.DrawLine(Pens.Black, 0, colHS, picBox.Width + rowHS, colHS)
g.DrawLine(Pens.Black, rowHS, 0, rowHS, picBox.Height + colHS)
g.DrawRectangle(Pens.Black, 0, 0, picBox.Width - 1, picBox.Height - 1)
For i = 0 To DGV.ColumnCount - 1
startY = 0
For j = 0 To DGV.RowCount - 1
' Write values
g.DrawString(DGV(i, j).Value, DGV.DefaultCellStyle.Font, Brushes.Black, startX + rowHS, startY + 2 + colHS)
' Write row numbers
g.DrawString(j.ToString, DGV.DefaultCellStyle.Font, Brushes.Black, 2, startY + 2 + colHS)
startY = startY + DGV.Rows(j).Height
g.DrawLine(Pens.Black, 0, startY + colHS, picBox.Width + rowHS, startY + colHS)
Next
'Define the rectangle for header text
lr.X = startX + rowHS
lr.Y = 2
lr.Width = DGV.Columns(i).Width
lr.Height = colHS
'Write header text
g.DrawString(DGV.Columns(i).HeaderText, DGV.RowHeadersDefaultCellStyle.Font, Brushes.Black, lr)
startX = startX + DGV.Columns(i).Width
g.DrawLine(Pens.Black, startX + rowHS, 0, startX + rowHS, picBox.Height + colHS)
Next
bmp = picBox.Image
Return bmp
End Function