//将图片存入数据库
private void button1_Click(object sender, EventArgs e)
{//点击浏览图片的按钮,选择图片
byte[] blob=null;//定义一个byte变量来存储图片的二进制编码
DialogResult result = openFileDialog1.ShowDialog();//openFileDialog是对话框控件
if (result == DialogResult.OK)
{
txtPho_path.Text = openFileDialog1.FileName;//txtPho_path是图片的路径名
pictureBox1.ImageLocation = openFileDialog1.FileName;//将选择的图片加载到pictureBox中,openFileDialog1.FileName是图片路径
//将选择的图片序列化存放在blob中
FileStream stream = new FileStream(openFileDialog1.FileName, FileMode.Open);
blob = new byte[stream.Length];
stream.Read(blob, 0, (int)stream.Length);//将图片的二进制编码读入blob中
stream.Close();
//接下来要做的就是把blob存入数据库就行了
}
}
//从数据库中取出图片
public LoadPicture()
{
DataSet ds=查询数据库返回一个DataSet;
byte[] blob=(byte[])ds.Tables[0].rows[0][存放图片二进制的字段名];//将图片的二进制流保存到blob中
MemoryStream stream = new MemoryStream(blob);
Image bitmap = Image.FromStream(stream);//将图片二进制流还原成图片
pictureBox1.Image = bitmap;//将图片绑定到图片控件上
}