首页 > Windows开发 > 详细

WinForm实现保存图片,读取图片(Stream流的形式)

时间:2015-04-15 19:41:15      阅读:455      评论:0      收藏:0      [点我收藏+]

直接将图片保存到数据库,可能会导致数据库压力比较大,当然这样有利于图片数据的迁移和备份。

这种方法只适合于保存用户头像等较小的图片。


//读取图片

           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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!