文档详情

将图片转换成二进制存储在数据库中.docx

发布:2017-07-07约3.73千字共4页下载文档
文本预览下载声明
将图片转换成二进制存储在数据库中using System;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;using System.IO;usingSystem.Data.SqlClient;namespace WindowsApplication1{public partial class Form1 : Form {stringstrPath;FileStream file;public Form1() {InitializeComponent(); } //先定义一个方法用于将传入的图片转换成二进制代码private Byte[] setPhotoByte(string photoPath) {if (photoPath != null ) { string str = photoPath;//保存传入的路径 //将传入路径下的图片以文件方式打开 file = new FileStream(str, FileMode.Open, FileAccess.Read);//以只读方式打开传入路劲下的图片Byte[] byteSQLData = new byte[file.Length];file.Read(byteSQLData, 0, byteSQLData.Length);//完整的将文件中的数据读入到创建的byte数据中file.Close();MessageBox.Show(photoPath);returnbyteSQLData; }else { if (File.Exists(@E:/学生党员信息管理系统/其它/默认相片.jpg) == true) { string str = @E:/学生党员信息管理系统/其它/默认相片.jpg;file = new FileStream(str, FileMode.Open, FileAccess.Read);Byte[] byteSQLData = new byte[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); string str = @E:/学生党员信息管理系统/其它/默认相片.jpg;file = new FileStream(str, FileMode.Open, FileAccess.Read);Byte[] byteSQLData = new byte[file.Length];file.Read(byteSQLData, 0, byteSQLData.Length);file.Close();MessageBox.Show(2 + photoPath);returnbyteSQLData; } } } //定义一个方法显示图片private void getPhotoByte() {SqlConnectionsqlCon = new SqlConnection(@Data Source=CHENHAO;Initial Catalog=学生党员信息管理系统;Integrated Security=True);sqlCon.Open();string sql1 = select photo from photo where photoID=+Convert .ToInt32 (this .textBox1 .Text .ToString ().Trim ());SqlCommandsqlCom =
显示全部
相似文档