从DatagridView创建一个Tiff位图文件 [英] Create a Tiff Bitmap file from a DatagridView
问题描述
我想从Datagridview创建一个Tiff文件。我能够将Datagridview转换为Tiff文件,但是我只想要行和列而已。
I want to create a Tiff file from a Datagridview. I was able to get the Datagridview to a Tiff file, however I just want the Rows and Columns and nothing else.
是否可以不使用第三方工具?
Is this possible without using 3rd party Tool?
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add(New String() {"Value1", "Value2", "Value3"})
Dim height As Integer = DataGridView1.Height
DataGridView1.Height = DataGridView1.RowCount * DataGridView1.RowTemplate.Height
Dim bitmap As Bitmap = New Bitmap(Me.DataGridView1.Width - 1, Me.DataGridView1.Height - 1)
DataGridView1.DrawToBitmap(bitmap, New Rectangle(0, 0, Me.DataGridView1.Width - 1, Me.DataGridView1.Height - 1))
'Save the Bitmap to folder.
bitmap.Save("C:Development\DataGridView.Tiff")
End Sub
我不希望突出显示
推荐答案
要考虑以下几点:
- DataGridView行和列控件绘制到位图时必须可见,
- 可能存在滚动条,
- 行的高度可能不同,所以我们必须对所有行的高度求和,
- 列的相同,因为每个列都有自己的宽度,
- 可以使用CellFormatting ,因此我们需要在绘制之前刷新DataGridView:不可见的行可能尚未格式化,
- 位图尺寸有限制(32,767)。
- The DataGridView Rows and Columns must be visible when the control is drawn to a Bitmap,
- ScrollBars may be present,
- The height of the Rows may be different, so we have to sum the height of all rows,
- The same for the Columns, since each Column has it's own width,
- CellFormatting may be in place, so we need to refresh the DataGridView before drawing it: rows that are not visible may not have been formatted yet,
- There's a limit (32,767) in the Bitmap dimensions.
致电t他的方法如下,指定要包含行标题还是列标题,还是要排除两者,并传递 True
/ False
作为 ColumnHeaders
和 RowHeaders
自变量。
dgv
参数当然是将绘制的DataGridView控件实例:
Call this method as follows, specifying whether you want to include the Row or Column Headers or exclude both, passing True
/False
as the ColumnHeaders
and RowHeaders
arguments.
The dgv
argument is of course the DataGridView control instance that will be drawn:
' Prints the DataGridView including the Columns' Headers only
Dim dgvBitmap = DataGridViewToBitmap(DataGridView1, True, False)
Dim imagePath = Path.Combine(AppContext.BaseDirectory, $"{NameOf(DataGridView1)}.tiff")
dgvBitmap.Save(imagePath, ImageFormat.Tiff)
' Dispose of the Bitmap or set it as the PictureBox.Image, dispose of it later.
dgvBitmap.Dispose()
Private Function DataGridViewToBitmap(dgv As DataGridView, ColumnHeaders As Boolean, RowHeaders As Boolean) As Bitmap
dgv.ClearSelection()
Dim originalSize = dgv.Size
dgv.Height = dgv.Rows.OfType(Of DataGridViewRow).Sum(Function(r) r.Height) + dgv.ColumnHeadersHeight
dgv.Width = dgv.Columns.OfType(Of DataGridViewColumn).Sum(Function(c) c.Width) + dgv.RowHeadersWidth
dgv.Refresh()
Dim dgvPosition = New Point(If(RowHeaders, 0, dgv.RowHeadersWidth), If(ColumnHeaders, 0, dgv.ColumnHeadersHeight))
Dim dgvSize = New Size(dgv.Width, dgv.Height)
If dgvSize.Height > 32760 OrElse dgvSize.Width > 32760 Then Return Nothing
Dim rect As Rectangle = New Rectangle(Point.Empty, dgvSize)
Using bmp As Bitmap = New Bitmap(dgvSize.Width, dgvSize.Height)
dgv.DrawToBitmap(bmp, rect)
If (dgv.Width > originalSize.Width) AndAlso dgv.ScrollBars.HasFlag(ScrollBars.Vertical) Then
dgvSize.Width -= SystemInformation.VerticalScrollBarWidth
End If
If (dgv.Height > originalSize.Height) AndAlso dgv.ScrollBars.HasFlag(ScrollBars.Horizontal) Then
dgvSize.Height -= SystemInformation.HorizontalScrollBarHeight
End If
dgvSize = New Size(dgvSize.Width - dgvPosition.X, dgvSize.Height - dgvPosition.Y)
dgv.Size = originalSize
Return bmp.Clone(New Rectangle(dgvPosition, dgvSize), PixelFormat.Format32bppArgb)
End Using
End Function
这篇关于从DatagridView创建一个Tiff位图文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!