i hope someone can help me with this problem.
so, i have a code that will print records in the data grid view and i hope to save that as image how do i make that happen, i search through the web but i dont see one that solve this problem. i even see one from stackoverflow but the approach is different.
this is the code:
Dim addressFont As New Font(dgvWarehouse.Font.FontFamily, 10, FontStyle.Regular, GraphicsUnit.Pixel)
Dim logoImage As Image = CType(My.Resources.ResourceManager.GetObject("logo1"), Image)
e.Graphics.DrawImage(logoImage, CInt((e.PageBounds.Width - 800) / 2), 10, 300, 180)
Dim companyname As String = "Sample"
Dim address As String = "Sample"
Dim number As String = "Sample"
Dim email As String = "Sample"
e.Graphics.DrawString(companyname, CompanyFont, Brushes.Black, CSng(e.PageBounds.Width - 1100 / 2), 60)
e.Graphics.DrawString(address, addressFont, Brushes.Black, CSng(e.PageBounds.Width - 1100 / 2), 90)
e.Graphics.DrawString(number, addressFont, Brushes.Black, CSng(e.PageBounds.Width - 1100 / 2), 105)
e.Graphics.DrawString(email, addressFont, Brushes.Black, CSng(e.PageBounds.Width - 1100 / 2), 120)
''
Dim headerFont As New Font(dgvWarehouse.Font.FontFamily, 15, FontStyle.Italic, GraphicsUnit.Pixel)
Dim actualWidth As Integer = dgvWarehouse.Columns.Cast(Of DataGridViewColumn).Sum(Function(c) c.Width)
Dim percentage As Decimal = CDec(((100 / actualWidth) * e.MarginBounds.Width) / 100)
Dim header As String = "Warehouse Inventory"
Dim footer As String
Dim startX As Integer = e.MarginBounds.Left - 30
Dim startY As Integer = e.MarginBounds.Top
Dim r As Rectangle
Dim szf As SizeF = e.Graphics.MeasureString(header, headerFont)
e.Graphics.DrawString(header, headerFont, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - szf.Width) / 2, startY - (r.Height - 80))
footer = "Page " & pageCounter.ToString
szf = e.Graphics.MeasureString(footer, headerFont)
e.Graphics.DrawString(footer, headerFont, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - szf.Width) / 2, e.MarginBounds.Bottom + 5)
startY += 100
''this is the text alignment
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
Dim gridFont As New Font(dgvWarehouse.Font.FontFamily, dgvWarehouse.Font.Size, FontStyle.Regular, GraphicsUnit.Pixel)
'' this create the table header column
'If startRow = 0 Then
For x As Integer = 0 To dgvWarehouse.Columns.Count - 1
r.X = startX
r.Y = startY
r.Width = CInt((dgvWarehouse.Columns(x).Width + 15) * percentage)
r.Height = dgvWarehouse.Rows(0).Height
dgvWarehouse.Columns(x).HeaderCell.Style.BackColor = Color.Gray
e.Graphics.FillRectangle(New SolidBrush(dgvWarehouse.Columns(x).HeaderCell.Style.BackColor), r)
e.Graphics.DrawRectangle(Pens.Black, r)
e.Graphics.DrawString(dgvWarehouse.Columns(x).HeaderText, gridFont, Brushes.White, r, sf)
startX += r.Width
Next
startY += r.Height
'End If
'' this create the table header row
For y As Integer = startRow To dgvWarehouse.Rows.Count - 1
If y = dgvWarehouse.NewRowIndex Then Continue For
startX = e.MarginBounds.Left
For x As Integer = 0 To dgvWarehouse.Columns.Count - 1
r.X = startX - 30
r.Y = startY
r.Width = CInt((dgvWarehouse.Columns(x).Width + 15) * percentage)
r.Height = dgvWarehouse.Rows(0).Height
e.Graphics.DrawRectangle(Pens.Black, r)
e.Graphics.DrawString(If(Not dgvWarehouse.Rows(y).Cells(x).Value Is Nothing, dgvWarehouse.Rows(y).Cells(x).Value.ToString, ""),
gridFont, Brushes.Black, r, sf)
startX += r.Width
Next
startY += r.Height
If startY >= e.MarginBounds.Bottom - 10 Then
If y < dgvWarehouse.Rows.Count - 1 Then
e.HasMorePages = True
pageCounter += 1
startRow = y + 1
Exit For
End If
End If
Next
i can just save the datagridview as image but that is not what i want. i want to save the one in print preview where there is an icon and has stylish table.