如何在鼠标点击时从datagridview加载图像到vb.net中的picturebox [英] how to load an image from datagridview on mouse click into picturebox in vb.net
本文介绍了如何在鼠标点击时从datagridview加载图像到vb.net中的picturebox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我正在开展一个项目,我有一个通过datagridview显示的图像列表。
我想加载在datagridview中单击特定图像时,图像进入图片框。
我的代码
Hi Everyone
I am working on a project where I have a list of images displayed through datagridview.
I want to load an image into picturebox when the particular image is clicked in datagridview.
My code
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
我的内存异常..
可能是什么问题。
I get Out of memory exception..
What may be the problem.
推荐答案
你好!
像魅力一样工作!
Hello !
Working like charm !
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim pCell As New DataGridViewImageCell
pCell = Me.DataGridView1.Item("ThumbNailPhotoDataGridViewImageColumn", e.RowIndex)
Me.PictureBox1.Image = byteArrayToImage(pCell.Value)
End Sub
Private Function byteArrayToImage(ByVal byt As Byte()) As Image
Dim ms As New System.IO.MemoryStream()
Dim drwimg As Image = Nothing
Try
ms.Write(byt, 0, byt.Length)
drwimg = New Bitmap(ms)
Finally
ms.Close()
End Try
Return drwimg
End Function
这篇关于如何在鼠标点击时从datagridview加载图像到vb.net中的picturebox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文