Results 1 to 3 of 3

Thread: Convert DataGridViewCell to Bitmap

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Convert DataGridViewCell to Bitmap

    I'm trying to get the bitmap of a DataGridViewCell. The best way to describe it is the DrawToBitmap method on most controls. Is there a built in method for this and if not has somebody already created a similar method?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Convert DataGridViewCell to Bitmap

    I'd probably calculate the size and (screen) coordinates and use Graphics.CopyFromScreen

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Convert DataGridViewCell to Bitmap

    Give this a try:

    VB.Net Code:
    1. Public Function GetDGVCellBitMap(ByVal dgv As DataGridView, ByVal row As Int32, ByVal col As Int32) As Bitmap
    2.    Return GetDGVCellBitMap(dgv.Item(col, row))
    3. End Function
    4.  
    5. Public Function GetDGVCellBitMap(ByVal cell As DataGridViewCell) As Bitmap
    6.    Dim dgv As DataGridView = cell.DataGridView
    7.    Dim firstDisplayedCell As DataGridViewCell = dgv.FirstDisplayedCell
    8.    dgv.FirstDisplayedCell = cell
    9.    Dim cellWasSelected As Boolean = cell.Selected
    10.    cell.Selected = False
    11.  
    12. '     get DataGridView method: GetCellAdjustedDisplayRectangle
    13. '     Friend Function GetCellAdjustedDisplayRectangle(ByVal columnIndex As Integer, ByVal rowIndex As Integer, ByVal cutOverflow As Boolean) As Rectangle
    14.  
    15.    Dim miGetCellAdjustedDisplayRectangle As Reflection.MethodInfo = GetType(DataGridView).GetMethod("GetCellAdjustedDisplayRectangle", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
    16.  
    17.    Dim cellBounds As Rectangle = CType(miGetCellAdjustedDisplayRectangle.Invoke(dgv, New Object() {cell.ColumnIndex, cell.RowIndex, False}), Rectangle)
    18.    cellBounds.Location = New Point(cellBounds.X, 0) ' Point.Empty
    19.  
    20.  
    21. '     get DataGridViewRow method: PaintCells
    22. '     Protected Friend Overridable Sub PaintCells(ByVal graphics As Graphics,
    23.                                               'ByVal clipBounds As Rectangle,
    24.                                               'ByVal rowBounds As Rectangle,
    25.                                               'ByVal rowIndex As Integer,
    26.                                               'ByVal rowState As DataGridViewElementStates,
    27.                                               'ByVal isFirstDisplayedRow As Boolean,
    28.                                               'ByVal isLastVisibleRow As Boolean,
    29.                                               'ByVal paintParts As DataGridViewPaintParts)
    30.  
    31.    Dim rowBounds As Rectangle = dgv.GetRowDisplayRectangle(cell.RowIndex, False)
    32.  
    33.    'offset rowbounds x by cellbounds x
    34.    rowBounds.Location = New Point(rowBounds.Location.X - cellBounds.Location.X, 0)
    35.    Dim ret As New Bitmap(cellBounds.Width, cellBounds.Height)
    36.  
    37.    Dim miPaintCells As Reflection.MethodInfo = GetType(DataGridViewRow).GetMethod("PaintCells", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
    38.    Using g As Graphics = Graphics.FromImage(ret)
    39.       g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    40.       g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
    41.       Dim row As DataGridViewRow = dgv.Rows(cell.RowIndex)
    42.       miPaintCells.Invoke(row, New Object() {g, _
    43.                                              rowBounds, _
    44.                                              rowBounds, _
    45.                                              cell.RowIndex, _
    46.                                              row.State, _
    47.                                              False, _
    48.                                              False, _
    49.                                              DataGridViewPaintParts.All})
    50.    End Using
    51.  
    52.    cell.Selected = cellWasSelected
    53.    dgv.FirstDisplayedCell = firstDisplayedCell
    54.    Return ret
    55. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width