字节转换回图像

Byte conversion back to Image

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

但是,方法 1 returns plr.PlayerImage 上的错误 "Value of type Byte cannot be converted to 1-dimensional array of Byte"。

方法 2 提供错误消息 "Conversion from type Byte() to type Byte is not valid"。

方法 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