-1

我正在做一个项目,我有一个通过 datagridview 显示的图像列表。当在 datagridview 中单击特定图像时,我想将图像加载到图片框中。

我的代码

Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
        sql.Open()

        cmd = New SqlCommand("select pic from detail4 where id='" & DataGridView1.CurrentRow.Cells(0).Value() & "'", sql)
        Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
        If Not imageData Is Nothing Then
            Using ms As New MemoryStream(imageData, 0, imageData.Length)
                ms.Write(imageData, 0, imageData.Length)
                PictureBox1.BackgroundImage = Image.FromStream(ms, True)
            End Using
        End If

    End Sub

我得到内存不足异常..可能是什么问题。请帮忙

4

1 回答 1

0

将 imagedata 声明为对象

Dim cmd As SqlCommand
cmd = New SqlCommand("select vchimage1  from producto where chrcodigoproducto='" & cCodProducto & "'", ClsCn.Cnx_Base)
Dim imagedata As Object
imagedata = cmd.ExecuteScalar()
'Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())

If IsNothing(imagedata) Or IsDBNull(imagedata) Then

    ImgProducto.Visible = False
    mLoadPictureImage = False
Else
    Using ms As New MemoryStream(imagedata, 0, imagedata.Length)
        ms.Write(imagedata, 0, imagedata.Length)
        ImgProducto.BackgroundImage = Image.FromStream(ms, True)

        mLoadPictureImage = True
    End Using
End If

www.jisoft-ingenieros.com

于 2014-03-28T04:03:11.030 回答