2016-01-07 68 views
0

我需要某段代碼來幫助我打印存儲在datagrid行或SQL數據庫圖像上的圖像,並且我想做一些有用的事情出來了。單擊datagridview行時在圖片框上顯示圖像

我的觀點是每次點擊數據行就插入圖像。

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(); 
    } 
} 
+0

什麼是您的具體問題? –

回答

0

假設您的數據來自字節數組,例如,包含有效圖像數據的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 

它是由你來上SizeModePictureBoxSize決定。您可以測試bmp.Size,如果你想..

不要忘記分配一個新的人之前先清理位:

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

非常感謝您的先生!我真的很喜歡它,我真的這樣做! :) –