【问题标题】:Trouble in converting an image from database to byte and back to image将图像从数据库转换为字节并返回图像时出现问题
【发布时间】:2015-09-28 08:44:39
【问题描述】:

我的表单中有一个按钮和一个 datagridview。我需要从数据库中获取图像并在数据网格中显示其值,然后在数据网格中获取图像的值以将其复制到另一个数据库。

具体来说,我将图像 (blob) 从 database1 的 table1 复制到 database2 的 table 1。

Button1_click:

Dim img As Image
    Dim bArr As Byte()
Try
            Dim Sql = "Select ID, IMG from sample"
            connectionOnline()
            Dim cmd = New MySqlCommand(Sql, ConOnline)
            Dim dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            DataGridView1.Rows.Clear()
            While dr.Read = True
                img = dr(1)
                bArr = imgToByteArray(img)
                DataGridView1.Rows.Add(dr(0), bArr)
            End While
            ConOnline.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Try
            connectionSync()
            Dim a, b As String
            Dim Sql = "INSERT INTO SAMPLE (ID, IMG)values(@a,@b)"

            For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
                a = Me.DataGridView1.Rows(i).Cells(0).Value.ToString()
                Dim img1 As Image = byteArrayToImage(bArr)
                Dim cmd As New MySqlCommand(Sql, ConSync)
                cmd.Parameters.AddWithValue("@a", a)
                cmd.Parameters.AddWithValue("@b", img1)
                cmd.ExecuteNonQuery()
                cmd.Parameters.Clear()
            Next
            ConSync.Close()
        Catch ex As Exception
               MsgBox(ex.Message)
        End Try


Try
            connectionSync()
            Dim Sql = "INSERT INTO B.SAMPLE(ID, IMG) SELECT ID, IMG FROM C.SAMPLE WHERE not exists (SELECT 1 from B.SAMPLE WHERE B.SAMPLE.ID=C.SAMPLE.ID)"
            Dim cmd = New MySqlCommand(Sql, ConSync)
            With cmd
                .ExecuteNonQuery()
            End With
            MsgBox("Success", vbInformation, "Save")
            ConSync.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Try
            connectionOffline()
            Dim Sql = "UPDATE SAMPLE SET IMG=(SELECT C.SAMPLE.NAME FROM C.SAMPLE WHERE C.SAMPLE.ID=B.SAMPLE.ID) WHERE B.SAMPLE.ID=(SELECT C.SAMPLE.ID FROM C.SAMPLE WHERE C.SAMPLE.ID=B.SAMPLE.ID)"
            Dim cmd = New MySqlCommand(Sql, ConOffline)
            With cmd
                .ExecuteNonQuery()
            End With
            MsgBox("Success")
            ConOffline.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

B 和 C 是数据库
而样本就是表格

下面是我用来转换图像的函数

Public Function imgToByteArray(ByVal img As Image) As Byte()
        Using mStream As New MemoryStream()
            img.Save(mStream, img.RawFormat)
            Return mStream.ToArray()
        End Using
    End Function

    Public Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
        Using mStream As New MemoryStream(byteArrayIn)
            Return Image.FromStream(mStream)
        End Using
    End Function

结果(在数据库中;IMG)只显示:“System.Drawing.Bitmap”而不是实际图像

【问题讨论】:

    标签: vb.net datagridview datagrid


    【解决方案1】:

    您只存储一个“Image.ToString”。

    尝试使用二进制参数:

    Dim yourParam As new SqlParameter("@b", System.Data.SqlDbType.Binary)
    yourParam.Direction = ParameterDirection.Input
    ' yourParam.value= yourImage   ' you can't place  a system.drawin.image here
    Dim ms As New MemoryStream()
    yourImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) 
    yourParam.value= ms.ToArray() ' similar to your imgToByteArray function .... 
    
    cmd.Parameters.Add(yourParam)
    

    【讨论】:

    • 我可以知道 your.value=bArr 是什么吗?另外,我得到一个“无法将'System.Byte []'类型的对象转换为'Sytem.Drawing.Image'。”错误:(
    • 无论如何,我在代码中删除了b = Me.DataGridView1.Rows(i).Cells(1).Value.ToString(),因为我在cmd.Parameters.AddWithValue("@b", img1) 中使用img 而不是b
    • @MiharuChan 更多信息在回答中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多