gpt4 book ai didi

vb.net - 字节转换回图像

转载 作者:行者123 更新时间:2023-12-02 17:34:24 24 4
gpt4 key购买 nike

我正在尝试将 plr.PlayerImage 中的字节转换回图片框的图像。

但是,方法1在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

最佳答案

正如我在上面的评论中所说,您没有将您的属性转换到您创建的内存流中。此外,如果 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

关于vb.net - 字节转换回图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28638002/

24 4 0