欢迎来到天天文库
浏览记录
ID:34762714
大小:50.52 KB
页数:4页
时间:2019-03-10
《将图片转换成二进制存储在数据库中》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、将图片转换成二进制存储在数据库中usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Data.SqlClient;namespaceWindowsApplication1{publicpartialclassForm1:Form{stringstrPath;
2、FileStreamfile;publicForm1(){InitializeComponent();}//先定义一个方法用于将传入的图片转换成二进制代码privateByte[]setPhotoByte(stringphotoPath){if(photoPath!=null){stringstr=photoPath;//保存传入的路径//将传入路径下的图片以文件方式打开file=newFileStream(str,FileMode.Open,FileAccess.Read);//以只读方式打开传入路劲下的图片Byte[]byteSQLData=newby
3、te[file.Length];file.Read(byteSQLData,0,byteSQLData.Length);//完整的将文件中的数据读入到创建的byte数据中file.Close();MessageBox.Show(photoPath);returnbyteSQLData;}else{if(File.Exists(@"E:/学生党员信息管理系统/其它/默认相片.jpg")==true){stringstr=@"E:/学生党员信息管理系统/其它/默认相片.jpg";file=newFileStream(str,FileMode.Open,File
4、Access.Read);Byte[]byteSQLData=newbyte[file.Length];file.Read(byteSQLData,0,byteSQLData.Length);file.Close();MessageBox.Show("1"+photoPath);returnbyteSQLData;}else{this.pictureBox1.Image.Save(@"E:/学生党员信息管理系统/其它/默认相片.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);stringstr=@"E:/学生党员
5、信息管理系统/其它/默认相片.jpg";file=newFileStream(str,FileMode.Open,FileAccess.Read);Byte[]byteSQLData=newbyte[file.Length];file.Read(byteSQLData,0,byteSQLData.Length);file.Close();MessageBox.Show("2"+photoPath);returnbyteSQLData;}}}//定义一个方法显示图片privatevoidgetPhotoByte(){SqlConnectionsqlCon=n
6、ewSqlConnection(@"DataSource=CHENHAO;InitialCatalog=学生党员信息管理系统;IntegratedSecurity=True");sqlCon.Open();stringsql1="selectphotofromphotowherephotoID="+Convert.ToInt32(this.textBox1.Text.ToString().Trim());SqlCommandsqlCom=newSqlCommand(sql1,sqlCon);if(Convert.DBNull!=sqlCom.Execute
7、Scalar()){pictureBox1.Image=Image.FromStream(newMemoryStream((Byte[])sqlCom.ExecuteScalar()));}sqlCon.Close();}//定义一个方法用于保存图片到数据库中privateintsetPhotToSave(Byte[]byteSQLData){SqlConnectionsqlCon=newSqlConnection(@"DataSource=CHENHAO;InitialCatalog=学生党员信息管理系统;IntegratedSecurity=True"
8、);sqlCon.Open();SqlCommandsqlCom=
此文档下载收益归作者所有