Reputation: 37
I'm trying to convert the bytes in plr.PlayerImage back into an image for the picturebox.
However, Method 1 returns the error "Value of type Byte cannot be converted to 1-dimensional array of Byte" on plr.PlayerImage.
Method 2 provides the error message "Conversion from type Byte() to type Byte is not valid".
Method 1 works when used in a separate sub where I retrieve the data from the database, but won't work in my new sub:
Dim pictureData As Byte() = DirectCast(drResult("PlayerImage"), Byte())
Dim picture As Image = Nothing
'Create a stream in memory containing the bytes that comprise the image.
Using stream As New IO.MemoryStream(pictureData)
'Read the stream and create an Image object from the data.'
picture = Image.FromStream(stream)
End Using
UC_Menu_Scout1.PictureBox1.Image = picture
Current Code:
Private Sub fillPlayerInfo()
For Each plr As Player In getAllPlayers()
If lbPlayers.SelectedItem.PlayerID = plr.PlayerID Then
txtFirstName.Text = plr.PlayerFirstName
txtSurname.Text = plr.PlayerLastName
txtPlaceOfBirth.Text = plr.PlaceOfBirth
cmbClub.SelectedValue = plr.ClubID
dtpDOB.Value = plr.DOB
'**********Method 1*********************************************
Dim pictureData As Byte() = DirectCast(plr.PlayerImage, Byte())
Dim picture As Image = Nothing
'Create a stream in memory containing the bytes that comprise the image.
Using stream As New IO.MemoryStream(pictureData)
'Read the stream and create an Image object from the data.
picture = Image.FromStream(stream)
End Using
'**********Method 2*********************************************
Dim ms As New IO.MemoryStream(plr.PlayerImage)
Dim returnImage As Image = Image.FromStream(ms)
pcbEditPlayer.Image = returnImage
End If
Next
End Sub
Upvotes: 2
Views: 18038
Reputation: 8004
As said in my comment from above, your not casting your property in the memory stream you have created. Also if plr.PlayerImage
is not defined as Byte()
you will get an exception.
Here's what it may look like...
Public Property PlayerImage As Byte()
Here's what you currently have...
Dim ms As New IO.MemoryStream(plr.PlayerImage) 'This is wrong...
Dim returnImage As Image = Image.FromStream(ms)
pcbEditPlayer.Image = returnImage
It should be like...
Dim ms As New IO.MemoryStream(CType(plr.PlayerImage, Byte())) 'This is correct...
Dim returnImage As Image = Image.FromStream(ms)
pcbEditPlayer.Image = returnImage
Upvotes: 6