首页 > 代码库 > 图片保存到数据库
图片保存到数据库
以Winform PictureBox控件为例
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Image img = Image.FromFile(openFileDialog1.FileName); //Image为C#中的图片对象 从文件中读取
this.img_Photo.Image = img;//让控件显示图片
MemoryStream ms = new MemoryStream();
img.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
objPerson.PER_PHOTO = ms.ToArray();//将流保存到数据库对象中、objPerson.PER_PHOTO是定义的一个IMage类型的字段
}
读取并显示
MemoryStream ms = new MemoryStream((byte[])objPerson.PER_PHOTO);
Image img = Image.FromStream(ms,true);//从流中读取
this.img_Photo.Image = img;
MemoryStream内从流可以参考博客园文档http://www.cnblogs.com/kissdodog/archive/2013/01/20/2868864.html
图片保存到数据库