二进制图片存储问题

本文介绍了一个实用的方法,将图片文件转换为二进制数据,并将其存储到数据库中。此外,还详细展示了如何从数据库中读取这些二进制数据并将其还原为图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先..定义一个函数..将图片转化为二进制码
//定义将图片转化为长二进制代码的函数getphoto()
         public Byte[] getphoto(string photopath)
         {
             string str = photopath;
             FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read);
             Byte[] bytBLOBData = new Byte[file.Length];
             file.Read(bytBLOBData, 0, bytBLOBData.Length);
             file.Close();
             return bytBLOBData;
         }//这是定义函数..

然后..就是将转换成二进制码的图片插入数据库中..下面是简单的也是重要的sql语句..
if (this.pictureBox1.Image != null)
                     {
                         sql1 = sql1 + ",Photo";
                         sql2 = sql2 + ",bytBLOBData";
                         Byte[] bytBLOBData = getphoto(openFileDialog1.FileName);
                         cmd.Parameters.Add(new OleDbParameter("jpeg", OleDbType.Binary, bytBLOBData.Length, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, bytBLOBData));
                     }

接下来..是读取...

string sql = "select photo from studentinfo where studentid = " + this.Tag.ToString();
             OleDbCommand cmd = new OleDbCommand(sql, connection1);
             if (Convert.DBNull != cmd.ExecuteScalar())
                 pictureBox1.Image = Image.FromStream(new MemoryStream((Byte[])cmd.ExecuteScalar()));//读取长二进制为图片..

转载于:https://www.cnblogs.com/guaiguaill0/p/3414651.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值