直接将图片保存到数据库,可能会导致数据库压力比较大,当然这样有利于图片数据的迁移和备份。
这种方法只适合于保存用户头像等较小的图片。
//读取图片
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); }
时间: 2024-10-05 04:55:08