C# 单击datagridview行时在图片框上显示图像

C# 单击datagridview行时在图片框上显示图像,c#,datagridview,C#,Datagridview,我需要一些代码来帮助我打印存储在datagrid行或SQL DB映像上的映像,我想从中做一些有用的事情 我的意思是每次单击数据行时插入一个图像 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Trabal

我需要一些代码来帮助我打印存储在datagrid行或SQL DB映像上的映像,我想从中做一些有用的事情

我的意思是每次单击数据行时插入一个图像

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Trabalhos\AuthMyRegistery\AuthMyRegistery\Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    id_Worker_Info = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["id_Worker_Info"].Value.ToString());
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from Worker where id_Worker_Info='" +id_Worker_Info+ "';";
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    sda.Fill(dt);
    dataGridView1.DataSource = dt;

    foreach (DataRow dr in dt.Rows)
    {
       WorkerIdTextBox.Text = dr["id_Worker_Info"].ToString();
       NameTextBox.Text = dr["Name"].ToString();
       FnameTextBox.Text = dr["Formation_Name"].ToString();
       BirthTextBox.Text = dr["Date_of_Birth"].ToString();
    }
 }

假设您的数据来自字节数组,例如包含有效图像数据的blob类型,则应该可以:

    using System.IO;
    ..

    byte[] iData = (byte[])dr[yourColumnIndexOrName];
    Bitmap bmp = null;
    if (iData != null)
       using (var ms = new MemoryStream(iData ))
       {
           bmp = new Bitmap(ms);
       } 
    pb_image.Image = bmp;  // display in a PictureBox
由您决定
PictureBox
SizeMode
Size
。如果需要,可以测试
bmp.Size

在分配新位图之前,不要忘记清理位图:

if (pb_image.Image != null)
{
    Image img = pb_image.Image;
    pb_image.Image = null;
    img.Dispose();
}

您的具体问题是什么?非常感谢先生!我真的很欣赏它,我真的很欣赏!:)