選取本地圖片文件加載到PictureBox中
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
if (ofd.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image = new Bitmap(ofd.FileName);
}
圖片轉化爲字節數組
如果將圖片存到數據庫,一種可以存儲圖片的路徑,但路徑可能變化,不推薦,更多的是直接存儲圖片的字節數組
using System.IO;
MemoryStream ms = new MemoryStream();
this.pictureBox1.Image.Save(ms, this.pictureBox1.Image.RawFormat);
byte[] imgBytes = ms.ToArray();
數據庫讀出字節數組並賦值給PictureBox
byte[] images = (byte[])dr["img"]; //從數據庫中讀出圖片所在欄位
MemoryStream ms = new MemoryStream(images);
pictureBox1.Image = Image.FromStream(ms);