vb.net 无法在 Windows 应用程序中将“System.Byte[]”类型的对象转换为“System.Drawing.Image”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21259068/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:30:58  来源:igfitidea点击:

Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image' in windows application

vb.netwinformsdatagridview

提问by user3106114

I have a data grid view,in update button i wrote code like this:

我有一个数据网格视图,在更新按钮中我写了这样的代码:

Dim cid As Integer
        Dim dtid As Integer
        Dim cmpname As String
        Dim dtname As String
        Dim dtPhone As String
        Dim dtEmail As String
        Dim dtimage As Image


        For i As Integer = 0 To gv.RowCount - 2
            ' Dim rv = DirectCast(bSource.Current, DataRowView)
            Dim rv = DirectCast(gv.Rows(i).DataBoundItem, DataRowView)
            cid = rv.Row.Field(Of Integer)("Cid")

            dtid = rv.Row.Field(Of Integer)("dtId")
            cmpname = rv.Row.Field(Of String)("CompanyName")
            dtname = rv.Row.Field(Of String)("Department")
            dtPhone = rv.Row.Field(Of String)("Phone")
            dtEmail = rv.Row.Field(Of String)("Email")
            dtimage = rv.Row.Field(Of Image)("empimage")

            adapter.UpdateCommand = New SqlCommand("UPDATE CompanyMaster_tbl SET CompanyName = @CompanyName", con.connect)
            adapter.UpdateCommand = New SqlCommand("update DepartmentMaster_tbl set dtName = @dtName,dtPhone = @dtPhone,dtEmail = @dtEmail,empimage=@dtimage  where dtId=@dtid", con.connect)
            adapter.UpdateCommand.Parameters.AddWithValue("@Cid", cid)

            adapter.UpdateCommand.Parameters.AddWithValue("@CompanyName", cmpname)
            adapter.UpdateCommand.Parameters.AddWithValue("@dtId", dtid)
            adapter.UpdateCommand.Parameters.AddWithValue("@dtName", dtname)
            adapter.UpdateCommand.Parameters.AddWithValue("@dtPhone", dtPhone)
            adapter.UpdateCommand.Parameters.AddWithValue("@dtEmail", dtEmail)
            adapter.UpdateCommand.Parameters.AddWithValue("@dtimage", dtimage)
            adapter.UpdateCommand.ExecuteNonQuery()

but i am getting error in this line dtimage = rv.Row.Field(Of Image)("empimage"):Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'

但我在这一行中遇到错误 dtimage = rv.Row.Field(Of Image)("empimage"): Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'

回答by Mike Perrenoud

I'm not sure the syntax is 100% right so I'll work on that, I'm a C# programmer by trade, but this is what you need to do:

我不确定语法是否 100% 正确,所以我会努力解决这个问题,我是一名 C# 程序员,但这是您需要做的:

Using ms As New MemoryStream(Row.Field(Of Byte())("empimage"))
    dtimage = New Bitmap(ms)
End Using


To save this same Bitmapback to the database you'll need to do this:

要将其保存Bitmap回数据库,您需要执行以下操作:

Using ms As New MemoryStream()
    bmp.Save(ms, ImageFormat.MemoryBmp)

    Dim bytes(ms.Length) As New Byte()
    ms.Read(bytes, 0, ms.Length)

    ' now save that Byte() to the field in the data table
End Using

NOTE:MemoryBmpmight not work--you may need to use something more specific. Here is a listing of them.

注意:MemoryBmp可能不起作用——您可能需要使用更具体的东西。这是它们清单

回答by Thomas Levesque

empimagecontains an array of bytes, you need to load the image from it:

empimage包含一个字节数组,您需要从中加载图像:

dtimage = ImageFromBytes(rv.Row.Field(Of Byte())("empimage"))

...


Function ImageFromBytes(ByVal bytes As Byte()) As Image
    Using ms As New MemoryStream(bytes)
        return Image.FromStream(ms)
    End Using
End Function