Eugene Santos
Eugene Santos

Reputation: 35

How to show an image from a DataGridView to a PictureBox?

I'm trying to show an image in my DataGridView in a PictureBox.
I'm using SQL Server as database.

Me.PictureBox2.Image = DataGridView2.Item(10, i).Value

I'm wondering what code I am using.

This is the error says:

Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'.

Upvotes: 1

Views: 621

Answers (1)

Jimi
Jimi

Reputation: 32248

You need to convert the Blob field Byte array (now a DataGridView cell Value) to an Image object.

A MemoryStream can be used to collect the Byte array and become the Stream source for the Image.FromStream() method.

If DataGridView2(10, 1).Value Is Nothing Then Return
Using ms As MemoryStream = New MemoryStream(CType(DataGridView2(10, i).Value, Byte()))
    PictureBox2.Image?.Dispose()
    PictureBox2.Image = Image.FromStream(ms)
End Using

Upvotes: 1

Related Questions