Jake Meister
Jake Meister

Reputation: 45

Display an image from datagridview to a picturebox

Can anyone help me with this one? The scenario is that when I click any columns in a datagridview it will display the image to a picturebox

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    If e.RowIndex >= 0 Then
        Dim row As DataGridViewRow
        row = Me.DataGridView1.Rows(e.RowIndex)

        lblProperty.Text = row.Cells("Column1").Value.ToString
        txtType.Text = row.Cells("Column2").Value.ToString
        txtPrice.Text = row.Cells("Column3").Value.ToString
        txtBed.Text = row.Cells("Column4").Value.ToString
        txtBath.Text = row.Cells("Column5").Value.ToString
        txtFootages.Text = row.Cells("Column6").Value.ToString
        txtStatus.Text = row.Cells("Column7").Value.ToString
        txtYear.Text = row.Cells("Column8").Value.ToString
        txtDesc.Text = row.Cells("Column9").Value.ToString

        Dim bytes As [Byte]() = row.Cells("Column10").Value
        Dim ms As New MemoryStream(bytes)
        pbImage.Image = Image.FromStream(ms)

        txtDate.Text = row.Cells("Column11").Value.ToString
        txtAddress.Text = row.Cells("Column12").Value.ToString
        txtStories.Text = row.Cells("Column13").Value.ToString

    End If
End Sub

It has an error Unable to cast object of type 'System.String' to type 'System.Byte[]'.

Upvotes: 0

Views: 6809

Answers (2)

Abdiaziz Abdi
Abdiaziz Abdi

Reputation: 21

Dim bytes As Byte() = Datagridview1.CurrentRow.Cells(5).Value
Using ms As New MemoryStream(bytes)
Picturebox1.Image = Image.FromStream(ms)
End Using

Upvotes: 1

Pure
Pure

Reputation: 158

Maybe you are over thinking it, there is a function to easily draw bitmaps.

I would suggest using the DrawToBitmap on the DataGridView by using the ColumnBounds example this:

Dim GridBitmap as new Bitmap(DataGridView1.GetColumnDisplayRectangle(row.Cells("Column10").ColumnIndex,false).width,DataGridView1.GetColumnDisplayRectangle(row.Cells("Column10").ColumnIndex,false).height)
DataGridView1.DrawToBitmap(GridBitmap,DataGridView1.GetColumnDisplayRectangle(row.Cells("Column10").ColumnIndex,false))
pbImage.Image = GridBitmap

It might need to be adapted to your DataGridView.

Note, Instead of this:

    Dim bytes As [Byte]() = row.Cells("Column10").Value
    Dim ms As New MemoryStream(bytes)
    pbImage.Image = Image.FromStream(ms)

Still having problem? Try this:

New project.
Add a DataGridView (DataGridView1) to your form.
Add some random columns to your gridview.
Add a Picture Box to your form (PictureBox1)
Make sure the Picture box property "SizeMode" is set to auto size.
Add an event forDataGridView1.MouseDown.
Add this code to the event.

Dim GridBitmap As New Bitmap(DataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, True).Width + DataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, True).Left, DataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, True).Height)
Dim GridColRectangle As New Rectangle(0, 0, DataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, True).Width + DataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, True).Left, DataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, True).Height + DataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, True).Top)
DataGridView1.DrawToBitmap(GridBitmap, GridColRectangle)
PictureBox1.Image = GridBitmap

You may need to adjust it to your own application.

Upvotes: 0

Related Questions