您的位置:首页 > 博客中心 > 数据库 >

图片保存到数据库

时间:2022-03-14 00:30

以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内从流可以参考博客园文档

热门排行

今日推荐

热门手游