标签:
直接将图片保存到数据库,可能会导致数据库压力比较大,当然这样有利于图片数据的迁移和备份。
这种方法只适合于保存用户头像等较小的图片。
//读取图片
if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
pathName = this.openFileDialog1.FileName; System.Drawing.Image img = System.Drawing.Image.FromFile(pathName); this.pictureBox1.Image = img;
<span style="white-space:pre"> </span>System.IO.FileStream fs = new System.IO.FileStream(pathName, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] buffByte = new byte[fs.Length]; fs.Read(buffByte, 0, (int)fs.Length); fs.Close(); fs = null;
}
<span style="white-space:pre"> </span> MemoryStream buf = new MemoryStream((byte[])stu[0].labPic); Image image = Image.FromStream(buf, true); this.pictureBox1.Image = image;
//这是操作数据库的部分
try { string sql = "insert into students(labPic,machineID,stuTime) values(@pic,@machine,@time)"; SqlParameter[] parameter = { new SqlParameter("@pic",stu.labPic), new SqlParameter("@machine",stu.machineID), new SqlParameter("@time",stu.stuTime) }; i = DbHelperSQL.ExecuteSql(sql, parameter); } catch (Exception ex) { WriteLog.WriteLogs(ex.Message); }
WinForm实现保存图片,读取图片(Stream流的形式)
标签:
原文地址:http://blog.csdn.net/xiaoyiyz/article/details/45062005