【问题标题】:Byte conversion back to Image字节转换回图像
【发布时间】: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


【解决方案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

【讨论】:

  • 如果有帮助,当我检索 PlayerImage 时: .PlayerImage = drResult("PlayerImage") .PlayerImage 绝对定义为字节
  • 解决了!尽管我的属性被定义为 Byte,但我将其更改为 Byte() 并且它起作用了!感谢您的帮助
  • 我在我的帖子(和第一条评论)中提到了这一点,你必须有 () 才能让它成为一个字节数组。此外,共识是如果帖子有帮助,请标记为已回答。
猜你喜欢
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 2018-11-30
相关资源
最近更新 更多