vb.net 字节转换回图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28638002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Byte conversion back to Image
提问by Nick
I'm trying to convert the bytes in plr.PlayerImage back into an image for the picturebox.
我正在尝试将 plr.PlayerImage 中的字节转换回图片框的图像。
However, Method 1 returns the error "Value of type Byte cannot be converted to 1-dimensional array of Byte" on plr.PlayerImage.
但是,方法1在plr.PlayerImage上返回错误“字节类型的值无法转换为字节的一维数组”。
Method 2 provides the error message "Conversion from type Byte() to type Byte is not valid".
方法 2 提供错误消息“从 Byte() 类型转换为 Byte 类型无效”。
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:
方法 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
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
回答by ???ěxě?
As said in my comment from above, your not casting your property in the memory stream you have created. Also if plr.PlayerImageis not defined as Byte()you will get an exception.
正如我在上面的评论中所说,您没有在您创建的内存流中投射您的财产。此外,如果plr.PlayerImage未定义为Byte()您将收到异常。
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

