专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WinForm

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

发布时间:2011-06-23 13:53:24 文章来源:www.iduyao.cn 采编人员:星星草
WinForm实现保存图片,读取图片(Stream流的形式)

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

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


//读取图片

           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);
            }




友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: