我正在尝试将plr.PlayerImage中的字节转换回图片框的图像。
但是,方法1在plr.PlayerImage上返回错误“类型字节的值无法转换为字节的1维数组”。
方法2提供错误消息“从类型字节转换()到字节类型无效”。
方法1在单独的子区域中使用时,我从数据库中检索数据,但在新的子组件中不起作用:
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
当前代码:
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
答案 0 :(得分:1)
正如我在上面的评论中所说,你没有在你创建的内存流中投射你的属性。此外,如果plr.PlayerImage
未定义为Byte()
,您将获得例外。
这里有什么样的......
Public Property PlayerImage As Byte()
以下是您目前拥有的 ...
Dim ms As New IO.MemoryStream(plr.PlayerImage) 'This is wrong...
Dim returnImage As Image = Image.FromStream(ms)
pcbEditPlayer.Image = returnImage
应该像 ...
Dim ms As New IO.MemoryStream(CType(plr.PlayerImage, Byte())) 'This is correct...
Dim returnImage As Image = Image.FromStream(ms)
pcbEditPlayer.Image = returnImage