如何在鼠标点击时从datagridview加载图像到vb.net中的picturebox

时间:2013-04-22 05:11:36

标签: vb.net datagridview picturebox

我正在开发一个项目,我有一个通过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

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

1 个答案:

答案 0 :(得分: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