[C#]將圖片存到資料庫中,再取出
2008-12-23 |
// Author: Blave Huang
MemoryStream ms = new MemoryStream();
Bitmap bmp = (Bitmap)this.pictureBox2.Image;
bmp.Save(ms, ImageFormat.Png); // 將圖片存到 MemoryStream
ms.Position = 0; // 移到最前面
string strEncoded = Convert.ToBase64String(ms.ToArray()); // 編成Base64
Console.WriteLine(strEncoded); // strEncoded 的內容可以存到Database
ms.Close();
MemoryStream ms2 = new MemoryStream(Convert.FromBase64String(strEncoded)); // 將資料庫中的編碼還原到MemoryStream
Bitmap bmp2 = new Bitmap(ms2);
this.pictureBox2.Image = bmp2; // 載入圖片
ms2.Close();