2012-08-01 8 views
1

嗨一個PictureBox我用下面的代碼將圖像保存到我的數據庫,獲取從MS Access中的圖片在C#

,但現在我想知道我可以從數據庫中獲取的圖片到PictureBox

你能幫我嗎。

private void button1_Click(object sender, EventArgs e) 
{ 
    ofdFoto.ShowDialog(); 
    string i = ofdFoto.FileName.ToString(); 
    pbxFoto.ImageLocation = i; 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    dbConn.Open(); 
    string querys = "INSERT INTO Fruits (Name, Picture) VALUES ('" + txtName.Text + "','" + ImageToByte(pbxFoto.Image) + "')"; 
    OleDbCommand cd = new OleDbCommand(querys, dbConn); 
    cd.ExecuteNonQuery(); 
    dbConn.Close(); 
    MessageBox.Show("Picture saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
} 

public static byte[] ImageToByte(Image img) 
{ 
    ImageConverter converter = new ImageConverter(); 
    return (byte[])converter.ConvertTo(img, typeof(byte[])); 
} 

回答

0
private void button1_Click(object sender, EventArgs e) 
    { 
     PictureBox p =new PictureBox(); 
     p.ImageLocation = ofdFoto.FileName.ToString(); 
     p.Location = new Point(100, 100); 
     this.Controls.Add(p); 
    } 

看看這可以幫助你!

0

試試這個代碼:

public static Bitmap BytesToBitmap(byte[] byteArray) 
{ 
    using (var ms = new MemoryStream(byteArray)) 
    { 
     var img = (Bitmap)Image.FromStream(ms); 
     return img; 
    } 
} 

,並設置爲PictureBox.Image屬性:

pictureBox1.Image = BytesToBitmap(byteArray); 
+0

我的代碼將圖像從字節轉換爲圖像,但我怎樣才能得到它變成一個PictureBox 。 – ArnoDT 2012-08-01 13:48:07

+0

我更新了我的答案 – Ria 2012-08-01 15:31:45

+0

謝謝,我如何從我的數據庫中獲取圖片 – ArnoDT 2012-08-01 16:07:28