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

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
file.Close();
MessageBox.Show("2" + photoPath);
return byteSQLData;
}
}
}
//定义一个方法显示图片
private void getPhotoByte()
{
SqlConnection sqlCon = new SqlConnection(@"Data Source=CHENHAO;Initial Catalog=学生党员信息管理系统;Integrated Security=True");
{
if (this.setPhotToSave(this.setPhotoByte(strPath)) > 0)//调用方法将图片以二进制的形式保存到数据库中
{
this.pictureBox1.Image = null ;
MessageBox.Show("图片保存成功");
}
}
private void buttonShow_Click(object sender, EventArgs e)
{
}
}
}
MessageBox.Show("1" + photoPath);
return byteSQLData;
}
else
{
this.pictureBox1.Image.Save(@"E:/学生党员信息管理系统/其它/默认相片.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
sqlCon.Open();
string sql1 = "select photo from photo where photoID="+Convert .ToInt32 (this .textBox1 .Text .ToString ().Trim ());
SqlCommand sqlCom = new SqlCommand(sql1, sqlCon);
{
InitializeComponent();
}
//先定义一个方法用于将传入的图片转换成二进制代码
private Byte[] setPhotoByte(string photoPath)
{
if (photoPath != null )
{
string str = photoPath;//保存传入的路径
将图片转换成二进制存储在数据库中
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
if (Convert.DBNull != sqlCom.ExecuteScalar())
{
pictureBox1 .Image =Image .FromStream (new MemoryStream ((Byte [])sqlCom .ExecuteScalar ()));
}
sqlCon.Close();
sqlCon.Open();
SqlCommand sqlCom = new SqlCommand("insert into photo (photo) values(@photo)", sqlCon);
SqlParameter sqlPar1 = new SqlParameter("@photo", SqlDbType.Binary, 8000);
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "JPEG(*.jpg)|*.jpg|位图文件(*.bmp)|*.bmp";//筛选图片格式
ofd.FilterIndex = 0;//从0开始查找
ofd.ReadOnlyChecked = false;//不是一个只读复选框
}
//定义一个方法用于保存图片到数据库中
private int setPhotToSave(Byte[] byteSQLData)
{
SqlConnection sqlCon = new SqlConnection(@"Data Source=CHENHAO;Initial Catalog=学生党员信息管理系统;Integrated Security=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(photoPath);
return byteSQLData;
}
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);//完整的将文件中的数据读入到创建的byte数据中
file = new FileStream(str, FileMode.Open, FileAccess.Read);
Byte[] byteSQLData = new byte[file.Length];
file.Read(byteSQLData, 0, byteSQLData.Length);
file.Close();
if (ofd.ShowDialog() == DialogResult.OK)//当用户选择完图片,并确认后
{
strPath = ofd.FileName;
this.pictureBox1.Image = Image.FromFile(this.strPath);
}
}
private void buttonSave_Click(object sender, EventArgs e)
sqlPar1.Value = byteSQLData;
sqlCom.Parameters.Add(sqlPar1);
int i= sqlCom.ExecuteNonQuery();
sqlCon.Close();
return i;
}Байду номын сангаас
private void buttonAdd_Click(object sender, EventArgs e)
{
this.getPhotoByte(); //调用方法显示保存在数据库中的图片
}
private void pictureBox1_Validated(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
using System.IO;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
string strPath;
FileStream file;
public Form1()
相关文档
最新文档