无法将类型为“System.Byte []”的对象强制转换为“System.Drawing.Image” [英] Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image

查看:531
本文介绍了无法将类型为“System.Byte []”的对象强制转换为“System.Drawing.Image”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一点帮助

i能够在我的访问数据库中保存几张图片,我还想浏览我的数据库

i可以用文本导航但对于图片而言,它有点难以因为我之前从未使用过图片

这里是我的导航代码

  private   Sub  NavigateRecords()
Dim da 作为 OleDb.OleDbDataAdapter( SELECT * FROM nax,con)
Dim dt As DataTable

da.Fill(dt)
如果 ds.Tables( tree)。Rows.Count> 0 然后
txtAgentNumber.Text = ds.Tables( tree)。行(inc).Item( 1
txtFirstName.Text = ds.Tables( tree)。Rows(inc).Item( 2
txtMiddleName.Text = ds.Tables( tree)。Rows(inc).Item( 3
txtSurName.Text = ds.Tables( tree)。Rows(inc).Item( 4
cboGender.Text = ds.Tables( tree)。Rows(inc ).Item( 5
txtAddress.Text = ds.Tables( tree)。Rows(inc).Item( 6
txtPhone.Text = ds.Tables( tree)。行(inc).Item( 7
txtEmail.Text = ds.Tables( tree)。Rows(inc ).Item( 8
txtNotes.Text = ds.Tables( tree)。Rows(inc).Item( 9
picPhoto.Image = ds.Tables( tree)。Rows(inc).Item( 10
ds.AcceptChanges()
结束 如果
结束 Sub

错误不断出现

error无法转换t的对象ype'System.Byte []'在'
picPhoto.Image = ds.Tables(tree)中键入'System.Drawing.Image'。行(inc).Item(10
i将非常感谢您的帮助

提前全部谢谢

解决方案

您无法将字节数组转换为图像完全 - 在大多数情况下它们甚至没有接近相同的大小!



相反,你需要将字节数组转换为内存流,然后构建图片来自:

  Dim  bytes  As  [ Byte ]()= ds.Tables(  tree)。Rows(inc).Item( 10 
Dim ms 作为 MemoryStream(字节)
picPhoto.Image = Image.FromStream(ms)


i need help here a little bit
i have been able to save several pictures in my access database and i also want to navigate through my database
i can navigate with text but for pictures it's kinda hard coz i have never used pictures before
here is my code for navigation

private Sub NavigateRecords()
    Dim da As New OleDb.OleDbDataAdapter("SELECT*FROM nax", con)
    Dim dt As New DataTable

    da.Fill(dt)
    If ds.Tables("tree").Rows.Count > 0 Then
        txtAgentNumber.Text = ds.Tables("tree").Rows(inc).Item(1)
        txtFirstName.Text = ds.Tables("tree").Rows(inc).Item(2)
        txtMiddleName.Text = ds.Tables("tree").Rows(inc).Item(3)
        txtSurName.Text = ds.Tables("tree").Rows(inc).Item(4)
        cboGender.Text = ds.Tables("tree").Rows(inc).Item(5)
        txtAddress.Text = ds.Tables("tree").Rows(inc).Item(6)
        txtPhone.Text = ds.Tables("tree").Rows(inc).Item(7)
        txtEmail.Text = ds.Tables("tree").Rows(inc).Item(8)
        txtNotes.Text = ds.Tables("tree").Rows(inc).Item(9)
        picPhoto.Image = ds.Tables("tree").Rows(inc).Item(10
        ds.AcceptChanges()
    End If
End Sub

the error keeps on appearing
error"Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image" at
picPhoto.Image = ds.Tables("tree").Rows(inc).Item(10
i would graetly appreciate your help
thanks all in advance

解决方案

You can't cast an array of bytes to an Image at all - they are not even close to the same size in most cases!

Instead, you need to convert the byte array to a Memory stream, then construct the Image from that:

Dim bytes As [Byte]() = ds.Tables("tree").Rows(inc).Item(10)
Dim ms As New MemoryStream(bytes)
picPhoto.Image = Image.FromStream(ms)


这篇关于无法将类型为“System.Byte []”的对象强制转换为“System.Drawing.Image”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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