您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 中卫分类信息网,免费分类信息发布

把图片保存到数据库中和从数据库中读取图片

2024/4/23 17:02:54发布2次查看
最近做到一个小项目,其中关系到图片的一些操作。比如:将图片保存到数据库中、从数据库中读取图片、显示图片、打印图片等。此处对这些在项目中遇到的一些琐碎知识加以总结,以便日后查找。 1、将图片作为其中的一个参数保存到数据库中 在项目中,一般是将图
最近做到一个小项目,其中关系到图片的一些操作。比如:将图片保存到数据库中、从数据库中读取图片、显示图片、打印图片等。此处对这些在项目中遇到的一些琐碎知识加以总结,以便日后查找。
1、将图片作为其中的一个参数保存到数据库中
在项目中,一般是将图片转换成二进制流格式,然后保存到数据库中。同时数据库表中存储图片的格式一般为image。此次项目,是将图片作为一个参数,和其他几个参数一起保存到数据库中,和在网上搜索到的图片保存不太一样,此处稍作修改,但都是检测过的。
存储步骤:
1、搜索到图片的路径
2、读取图片并将图片转换成二进制流格式
3、sql语句保存到数据库中。
 贴代码:
private void btnwrite_click(object sender, eventargs e) { openfiledialog ofd = new openfiledialog(); ofd.filter = *jpg|*.jpg|*.gif|*.gif|*.bmp|*.bmp; if (ofd.showdialog() == dialogresult.ok) { string filepath = ofd.filename;//图片路径 filestream fs = new filestream(filepath, filemode.open); byte[] imagebytes = new byte[fs.length]; binaryreader br = new binaryreader(fs); imagebytes = br.readbytes(convert.toint32(fs.length));//图片转换成二进制流 string strsql = string.format(insert into [sbs].[dbo].[model] ([m_qrcode],[m_skills] ) values (@image,'2')); int count = write(strsql,imagebytes ); if (count > 0) { messagebox.show(success); } else { messagebox.show(failed); } } }
数据库连接和保存图片语句:
private int write(string strsql,byte[] imagebytes) { string connstr = data source=192.168.4.132;initial catalog=sbs;user id=sa;password=sa;; using (sqlconnection conn = new sqlconnection(connstr)) { using (sqlcommand cmd = new sqlcommand(strsql, conn)) { try { conn.open(); sqlparameter sqlparameter = new sqlparameter(@image, sqldbtype.image); sqlparameter.value = imagebytes; cmd.parameters.add(sqlparameter); int rows = cmd.executenonquery(); return rows; } catch (exception e) { throw; } } } }
view code 
2、从数据库总读取图片
从数据库中读取图片字段,并转换成内存流生成bitmap。
贴代码:
private void btnread_click(object sender, eventargs e) { string strsql = string.format(select m_qrcode from [sbs].[dbo].[model] where m_id = 7);//图片保存的字段是m_qrcode read(strsql); } private void read(string strsql) { string connstr = data source=192.168.4.132;initial catalog=sbs;user id=sa;password=sa;; using (sqlconnection conn = new sqlconnection(connstr)) { using (sqlcommand cmd = new sqlcommand(strsql, conn)) { conn.open(); sqldatareader sqldr = cmd.executereader(); sqldr.read(); byte[] images = (byte[])sqldr[m_qrcode]; memorystream ms = new memorystream(images); bitmap bmp = new bitmap(ms); picturebox1.image = bmp; } } }
3、根据图片路径显示图片
这个比较简单,直接贴出代码
private void btnload_click(object sender, eventargs e) { openfiledialog ofd = new openfiledialog(); ofd.filter = *jpg|*.jpg|*.gif|*.gif|*.bmp|*.bmp; if (ofd.showdialog() == dialogresult.ok) { picturebox1.image = image.fromfile(ofd.filename); } }
4、打印图片
打印图片是在将图片显示在picturebox的基础上进行的。
步骤:
1、将printdocument控件拖到界面,添加打印代码
2、设置printdocument控件的print_printpage事件
private void btnprint_click(object sender, eventargs e) { printdialog printdialog = new printdialog(); printdialog.document = this.printdocument1; if (printdialog.showdialog() == dialogresult.ok) { try { printdocument1.print(); } catch (exception ex) { printdocument1.printcontroller.onendprint(printdocument1, new system.drawing.printing.printeventargs()); } } } private void printdocument1_printpage(object sender, system.drawing.printing.printpageeventargs e) { e.graphics.drawimage(picturebox1.image, 30, 30); }
附带着将图片转换成二进制和将二进制转换成图片专门写出来,以便于查看。
public byte[] convertbinary(string filepath) { filestream fs = new filestream(filepath, filemode.open, fileaccess.read);//以文件流形式读取图片 binaryreader br = new binaryreader(fs);//转换成二进制流 byte[] imagebytes = br.readbytes((int)fs.length);//保存到字节数组中 return imagebytes; } public void showimage(byte[] imagebytes) { memorystream ms = new memorystream(imagebytes); picturebox1.image = image.fromstream(ms); }
在picturebox中显示图片的三种方式:
public void method() { memorystream ms; picturebox1.image = image.fromstream(ms); bitmap bitmap; picturebox1.image = bitmap; string filepath; picturebox1.image = image.fromfile(filepath); }
winform中控件combobox控件使用:
public void bindcombobox() { datatable dt = new datatable(); dt.columns.add(new datacolumn(id, typeof(int))); dt.columns.add(new datacolumn(value, typeof(string))); for (int i = 0; i 3; i++) { datarow dr = dt.newrow(); dr[id] = i; dr[value] = 10 + i; dt.rows.add(dr); } this.combobox1.datasource = dt; this.combobox1.displaymember = value; this.combobox1.valuemember = id; } public void showvalue() { this.textbox1.text = this.combobox1.text; this.textbox2.text = this.combobox1.selectedvalue.tostring(); }
以上就是一些琐碎的总结,谨作为日后学习工作使用。
中卫分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录