字节转换回图像 [英] Byte conversion back to Image

查看:85
本文介绍了字节转换回图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to convert the bytes in plr.PlayerImage back into an image for the picturebox.

但是,方法1在plr.PlayerImage上返回错误字节类型的值不能转换为字节的一维数组。

However, Method 1 returns the error "Value of type Byte cannot be converted to 1-dimensional array of Byte" on plr.PlayerImage.

方法2提供了错误消息从类型Byte()转换为字节类型无效。

Method 2 provides the error message "Conversion from type Byte() to type Byte is not valid".

方法1在一个单独的子目录,我从数据库中检索数据,但在新的子目录中将不起作用:

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:

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(),则会出现异常。

As said in my comment from above, your not casting your property in the memory stream you have created. Also if plr.PlayerImage is not defined as Byte() you will get an exception.

这是它的外观...

 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

这篇关于字节转换回图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆