把图片转换成二进制--把二进制转换成图片

精贴 置顶
599 0

private void button1_Click(object sender, EventArgs e)
        {
           
string path = this.textBox1.Text;
           
byte[] imgBytesIn = SaveImage(path);
            ShowImgByByte(imgBytesIn);
           
//Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn;
          
        }
       
//将图片以二进制流
        public byte[] SaveImage(String path)
        {           
            FileStream fs
= new FileStream(path, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
            BinaryReader br = new BinaryReader(fs);
           
byte[] imgBytesIn = br.ReadBytes((int)fs.Length);  //将流读入到字节数组中
            return imgBytesIn;
        }
       
//现实二进制流代表的图片
        public void ShowImgByByte(byte[] imgBytesIn)
        {
            MemoryStream ms
= new MemoryStream(imgBytesIn);
            pictureBox1.Image
= Image.FromStream(ms);
        }

二、将图片保存到数据库中,并从数据库中读取:

#region 将图片从数据库中读取
        /// <summary>
        /// 将图片从数据库中读取
        /// </summary>
        /// <param name="xs_ID">要读取图片的学号</param>
        /// <param name="ph">pictureBox1控件名</param>
        public void get_photo(string xs_ID, PictureBox ph)//将图片从数据库中读取
        {
            byte[] imagebytes = null;
            getcon();
            SqlCommand con = new SqlCommand("select * from S_jiben where S_num='" + xs_ID + "'", link);
            SqlDataReader dr = con.ExecuteReader();
            while (dr.Read())
            {
                imagebytes =(byte[])dr.GetValue(18);
            }
            dr.Close();
            con_close();
            MemoryStream ms = new MemoryStream(imagebytes);
            Bitmap bmpt = new Bitmap(ms);
            ph.Image = bmpt; 
        }
        #endregion
        #region
        public void SaveImage(string MID, OpenFileDialog openF)//将图片以二进制存入数据库中
        {
            string strimg = openF.FileName.ToString();  //记录图片的所在路径
            FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
            BinaryReader br = new BinaryReader(fs);
            byte[] imgBytesIn = br.ReadBytes((int)fs.Length);  //将流读入到字节数组中
            getcon();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update S_jiben Set xs_photo=@Photo where S_num=" + MID);
            SqlCommand cmd = new SqlCommand(strSql.ToString(), link);
            cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn;
            cmd.ExecuteNonQuery();
            con_close();
        }
        #endregion

  • 没有任何评论
今日天气 ···

···

···

···

热门排行
CSS cursor鼠标样式一览表 2012/6/25
.NET后台写JS代码 2011/11/3
网站地址多出jdfwkey的问题解析及... 2010/8/25
百度、google、Yahoo网站地图制作... 2011/1/30
数据库xxx的日志已满,请备份该数... 2013/7/19
<a>标签的伪类书写顺序问题... 2010/9/4
CEO名言 2010/8/31
什么是长尾关键词? 2010/9/24
Mysql 主从数据库同步 2010/9/12
用ASP实现网页BBS 2010/11/1
博主推荐
网站title标题如何正确修改不会被... 2013/8/26
ASP .NET MYSQL 的简单分页 并不... 2013/8/19
网站有弹窗广告这样的站点,百度... 2013/8/17
做淘宝SEO优化需要注意的8大问题 2013/8/17
淘宝网怎么做SEO优化 2013/8/17
站长们要学习的“苍井空精神” 2013/8/17
苹果公司今日发布了iOS 7第五个开... 2013/8/7
小米1刷V5系统WIFI不能连接解决办... 2013/8/1
本博客手机版上线咯!会员后台也... 2013/7/31
Win7系统环境安装配置PHP开发环境... 2013/7/29
随便看看
用Response.Redirect打开一个新窗... 2010/8/27
将黑客扼杀在从注册表路径里 2010/10/15
<a>标签的伪类书写顺序问题... 2010/9/4
百度为何原创不收录 2013/6/23
win2003设置大全 2012/7/26
content = "IE=edge,chrome=1" 详... 2013/10/23
SQL快捷键大全 2010/9/9
xp进程全解 2010/8/31
XML Sitemap 相关问题 2010/9/26
行动者宣言 2013/11/2
北京 上海 杭州 深圳 广州 成都