这是我的解决方案。它可以正常工作,我可以从 DataGridView 中检索图像以加载到 PictureBox 中。
表单事件:
Private con As New SqlConnection("YourConnectionString")
Private com As SqlCommand
Private Sub DGV_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellClick
con.Open()
com = New SqlCommand("SELECT MyPhoto FROM tbGalary WHERE ID=" & DGV.Rows(e.RowIndex).Cells(0).Value, con)
Dim ms As New MemoryStream(CType(com.ExecuteScalar, Byte()))
txtPicture.Image = Image.FromStream(ms)
txtPicture.SizeMode = PictureBoxSizeMode.StretchImage
com.Dispose()
con.Close()
End Sub
SQL 表:
CREATE TABLE [dbo].[tbGalary](
[ID] [int] NOT NULL,
[MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
SQL插入图像:
INSERT INTO tbGalary VALUES('1','D:\image1.jpg')
INSERT INTO tbGalary VALUES('2','D:\image2.jpg')
INSERT INTO tbGalary VALUES('3','D:\image3.jpg')
INSERT INTO tbGalary VALUES('4','D:\image4.jpg')
INSERT INTO tbGalary VALUES('5','D:\image5.jpg')
结果
视频链接:Retrieve a image in DataGridView load to PictureBox in VB.NET