选取本地图片文件加载到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);