2013-05-18 40 views
3

我有一個轉換爲字節數組的sql表中的圖像。當我點擊包含該表的dataGridView的記錄時,如何將它顯示在圖片框中?我需要實際的代碼。謝謝。代碼我對轉換:如何從一個sql table中顯示一個字節數組圖像的圖片框?

private void button2_Click(object sender, EventArgs e) 
{ 
    SqlConnection cn = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True"); 
    MemoryStream ms = new MemoryStream(); 
    pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 

    byte[] pic_arr = new byte[ms.Length]; 
    ms.Position = 0; 
    ms.Read(pic_arr, 0, pic_arr.Length); 

    SqlCommand cmd = new SqlCommand("insert into motociclete(firma,model,poza,pret,anf,greutate,caprez,putere,garantie,stoc) values (@firma,@model,@poza,@pret,@anf,@greutate,@caprez,@putere,@garantie,@stoc)",cn); 
    cmd.Parameters.AddWithValue("@firma", textBox3.Text); 
    cmd.Parameters.AddWithValue("@model", textBox10.Text); 
    cmd.Parameters.AddWithValue("@poza", pic_arr); 
    cmd.Parameters.AddWithValue("@pret", textBox7.Text); 
    cmd.Parameters.AddWithValue("@anf", textBox4.Text); 
    cmd.Parameters.AddWithValue("@greutate", textBox9.Text); 
    cmd.Parameters.AddWithValue("@caprez", textBox5.Text); 
    cmd.Parameters.AddWithValue("@putere", textBox8.Text); 
    cmd.Parameters.AddWithValue("@garantie", textBox6.Text); 
    cmd.Parameters.AddWithValue("@stoc", textBox2.Text); 

    cn.Open(); 

    try 
    { 
     int rez = cmd.ExecuteNonQuery(); 
     if (rez > 0) 
     { 
      MessageBox.Show("Adaugare reusita "); 
     } 
    } 
    catch (Exception) 
    { 
     MessageBox.Show("Inregistrarea exista deja "); 
    } 
    finally 
    { 
     cn.Close(); 
     obj.loaddata(); 
     this.Close(); 
    } 
} 
+1

你能告訴你現在的代碼?如果我們看到你如何解決這個問題,那麼幫助你更容易。 – xxbbcc

+0

我只有將圖像轉換爲字節的代碼。我會發布。缺少 – stefan9976

回答

5

假設picturebyte[]是從數據庫中提取:

byte[] picture = // read from db 
PicBoxImage.Image = Image.FromStream(new MemoryStream(picture));         
PicBoxImage.Refresh(); 
+0

參數 –

相關問題