C# 将数据网格视图中的图像获取到c中的picturebox#

C# 将数据网格视图中的图像获取到c中的picturebox#,c#,datagridview,picturebox,C#,Datagridview,Picturebox,我需要在数据gridview中获得一个图像到c中的picturebox# 这是我的datagridview连接线 if (e.RowIndex >= 0) { DataGridViewRow row = this.dataGridView3.Rows[e.RowIndex]; e_id.Text = row.Cells["emp_id"].Value.ToString(); e_Fname.Text = row.Cells["emp_Fname"].Value.

我需要在数据gridview中获得一个图像到c中的picturebox#

这是我的datagridview连接线

if (e.RowIndex >= 0)
{

    DataGridViewRow row = this.dataGridView3.Rows[e.RowIndex];

    e_id.Text = row.Cells["emp_id"].Value.ToString();
    e_Fname.Text = row.Cells["emp_Fname"].Value.ToString();
    e_Lname.Text = row.Cells["emp_Lname"].Value.ToString();

}
我需要单击datagridview中的一行,然后在picturebox中加载图像。

使用以下方法:

pictureBox1.Image = (Image)dataGridView3.Rows[0].Cells[1].Value;
根据您的列和行在此处使用索引。

使用以下方法:

pictureBox1.Image = (Image)dataGridView3.Rows[0].Cells[1].Value;

根据您的列和行在此处使用索引。

如果您使用ImageList存储图像,我在本例中要做的是将图像索引存储在单元格的Tag属性中。

如果您使用ImageList存储图像,在这种情况下,我要做的是将图像索引存储在单元格的Tag属性中。

这是我的解决方案。从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
CREATE TABLE [dbo].[tbGalary](
    [ID] [int] NOT NULL,
    [MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
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')
SQL表:

 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
CREATE TABLE [dbo].[tbGalary](
    [ID] [int] NOT NULL,
    [MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
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')
SQL插入图像:

 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
CREATE TABLE [dbo].[tbGalary](
    [ID] [int] NOT NULL,
    [MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
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')
结果

视频链接:

这是我的解决方案。从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
CREATE TABLE [dbo].[tbGalary](
    [ID] [int] NOT NULL,
    [MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
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')
SQL表:

 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
CREATE TABLE [dbo].[tbGalary](
    [ID] [int] NOT NULL,
    [MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
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')
SQL插入图像:

 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
CREATE TABLE [dbo].[tbGalary](
    [ID] [int] NOT NULL,
    [MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
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')
结果


视频链接:

我知道已经很晚了,但我还是很想分享我的解决方案

关于datagrid的属性。你会发现点击

 private void sampleGrid_CellClick(object sender, DataGridViewCellEventArgs e)
            {
               string picpath = @"C:\Users\Public\Pictures\FolderOfYourImages\";
               //"emp_Lname" or the column index that you want to use               
               picpath = picpath + sampleGrid["emp_Lname", grdTrans.CurrentCell.RowIndex].Value.toString() + ".jpg"
               picbox.ImageLocation = picpath;
            }
当您单击datagrid时。它将更改图片框中的图像。对于我的示例,我选择LastName作为图像名称。但这将取决于你,它将只是一个简单的连接。最后,只需确保图像存在于您的路径中


希望这仍然有帮助:)快乐的编码。

我知道已经很晚了,但我还是很想分享我的解决方案

关于datagrid的属性。你会发现点击

 private void sampleGrid_CellClick(object sender, DataGridViewCellEventArgs e)
            {
               string picpath = @"C:\Users\Public\Pictures\FolderOfYourImages\";
               //"emp_Lname" or the column index that you want to use               
               picpath = picpath + sampleGrid["emp_Lname", grdTrans.CurrentCell.RowIndex].Value.toString() + ".jpg"
               picbox.ImageLocation = picpath;
            }
当您单击datagrid时。它将更改图片框中的图像。对于我的示例,我选择LastName作为图像名称。但这将取决于你,它将只是一个简单的连接。最后,只需确保图像存在于您的路径中


希望这仍然有帮助:)快乐的编码。

令人惊讶的是,上面没有人给出正确的答案。 上面的请求是:“我需要单击datagridview中的一行,然后在picturebox中加载图像。”

以下是C#中的正确方法,或者只是使用CType转换为VB的字节数组:


MikeB443[kd2cmo]

令人惊讶,但上面没有人给出正确答案。 上面的请求是:“我需要单击datagridview中的一行,然后在picturebox中加载图像。”

以下是C#中的正确方法,或者只是使用CType转换为VB的字节数组:


MikeB443[kd2cmo]

您的图像字段在哪里
emp\u Fname
emp\u Lname
?图像的字段在哪里<代码>emp\u Fname或
emp\u Lname
?谢谢。但我犯了这个错误。无法将类型为“System.Byte[]”的对象强制转换为类型为“System.Drawing.Image”。@Shaaji如何将图像保存到
gridview
?出示密码谢谢。但我犯了这个错误。无法将类型为“System.Byte[]”的对象强制转换为类型为“System.Drawing.Image”。@Shaaji如何将图像保存到
gridview
?显示代码基于我在他的代码上看到的图像是字节格式的。为什么要投反对票??它只是声明“我需要在datagridview中单击一行,然后在picturebox中加载图像。”。它没有提到它是字节格式的。根据我在他的代码中看到的,图像是字节格式的。为什么不投票??它只是声明“我需要在datagridview中单击一行,然后在picturebox中加载图像。”。它没有提到它是以字节为单位的。