none
how to convert system.byte[] in datagridview to Picturebox in my code RRS feed

  • Question

  • I use column type image in sql server , I want where click in row (45) show image I try with my code but not working  :

            Dim row As DataGridViewRow = DataGridView1.CurrentRow
            Dim Picture As Image = row.Cells(45).Value
            PictureBox1.Image = Picture

    Monday, May 15, 2017 7:46 AM

Answers

  • Hi,

    maybe [I dont know, if the IsDBNull function is the recommended way in vb nowadays...]

        Dim bmp As Bitmap = Nothing
            Dim row As DataGridViewRow = DataGridView1.CurrentRow
    
            If Not row.Cells(45).Value Is Nothing AndAlso Not IsDBNull(row.Cells(45).Value) Then
                Dim data As Byte() = row.Cells(45).Value
    
                Using ms As New IO.MemoryStream(data)
                    bmp = New Bitmap(ms)
                End Using
    
                Me.PictureBox1.Image = bmp
            End If

    Regards,

      Thorsten

    • Marked as answer by srajmuneer Monday, May 15, 2017 10:33 AM
    Monday, May 15, 2017 10:26 AM
  • ... I just see that checking for dbnull is typically done this way:

    IfNot DbNull.Value.Equals(row.Item(fieldName)) Then ...

    so you probably want to update the above code.

    see:

    https://msdn.microsoft.com/en-us/library/system.dbnull(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

    Regards,

      Thorsten


    Monday, May 15, 2017 10:30 AM

All replies

  • Hi,

    if the data is a byte(), you can load it with a memorystream:

            Dim bmp As Bitmap = Nothing
            Dim row As DataGridViewRow = DataGridView1.CurrentRow
            Dim data As Byte() = row.Cells(45).Value
    
            Using ms As New IO.MemoryStream(data)
                bmp = New Bitmap(ms)
            End Using
    
            Me.PictureBox1.Image = bmp

    Regards,

      Thorsten

    Monday, May 15, 2017 8:17 AM
  • thanks , if (row.Cells(45).Value) is DBNull who to this
    • Edited by srajmuneer Monday, May 15, 2017 10:16 AM
    Monday, May 15, 2017 10:15 AM
  • Hi,

    maybe [I dont know, if the IsDBNull function is the recommended way in vb nowadays...]

        Dim bmp As Bitmap = Nothing
            Dim row As DataGridViewRow = DataGridView1.CurrentRow
    
            If Not row.Cells(45).Value Is Nothing AndAlso Not IsDBNull(row.Cells(45).Value) Then
                Dim data As Byte() = row.Cells(45).Value
    
                Using ms As New IO.MemoryStream(data)
                    bmp = New Bitmap(ms)
                End Using
    
                Me.PictureBox1.Image = bmp
            End If

    Regards,

      Thorsten

    • Marked as answer by srajmuneer Monday, May 15, 2017 10:33 AM
    Monday, May 15, 2017 10:26 AM
  • ... I just see that checking for dbnull is typically done this way:

    IfNot DbNull.Value.Equals(row.Item(fieldName)) Then ...

    so you probably want to update the above code.

    see:

    https://msdn.microsoft.com/en-us/library/system.dbnull(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

    Regards,

      Thorsten


    Monday, May 15, 2017 10:30 AM
  • thanks Thorsten Gudera. can you pass value (bmp) in your code instead of (dr.Cells("image").Value) to view image , who this

    Dim dt As New DataTable
            With dt
                .Columns.Add("names")
                .Columns.Add("address")
                .Columns.Add("image", System.Type.GetType("System.Byte[]"))
            End With
            For Each dr As DataGridViewRow In Me.DataGridView1.Rows
                dt.Rows.Add(dr.Cells("names").Value, dr.Cells("address").Value, dr.Cells("image").Value)
            Next




    • Edited by srajmuneer Monday, May 15, 2017 6:54 PM
    Monday, May 15, 2017 6:52 PM