C#圖片Image的常規處理

選取本地圖片文件加載到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);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章