【发布时间】:2015-04-22 15:56:58
【问题描述】:
我正在尝试将 plr.PlayerImage 中的字节转换回图片框的图像。
但是,方法一在plr.PlayerImage上返回错误“Byte类型的值不能转换为Byte的一维数组”。
方法 2 提供错误消息“从类型 Byte() 到类型 Byte 的转换无效”。
方法 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
【问题讨论】:
-
在方法 2 中,您忘记将“plr.PlayerImage”包装在 Byte() 中。例如:plr.PlayerImage 应该是:Ctype(plr.PlayerImage, Byte()) 进入你的内存流。
-
我试过这个:Dim ms As New IO.MemoryStream(CType(plr.PlayerImage, Byte())),但我仍然收到错误“字节类型的值不能转换为一维plr.PlayerImage 上的字节数组
-
playerImage 是否定义为字节?
-
是的,在我的 Player 类中它被定义为 Byte
标签: vb.net type-conversion byte bytearray