在数据库中保存和读取图片
来源:互联网 发布:网络十大禁书全文下载 编辑:程序博客网 时间:2024/05/03 10:34
using System.IO
数据库结构(Imagelist(image))
添加OpenFileDialog1控件
将图片存入数据库。
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
string fullpath =openFileDialog1.FileName;//文件路径
FileStream fs = new FileStream(fullpath, FileMode.Open);
byte[] imagebytes =new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
//打开数据库
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=***;database=***");
con.Open();
//SqlCommand com = new SqlCommand("insert into ****** values(@ImageList)",con);
//这句是插入,如果已有记录,仅仅是插入照片,就不合适了,会有错误提示,这里的emp是变量,请根据自己的情况修改。
SqlCommand com = new SqlCommand("update tablename set column=@ImageList where EMPID='"+ emp +"'")",con);//更新图片
com.Parameters.Add("ImageList", SqlDbType.Image);
com.Parameters["ImageList"].Value = imagebytes;
com.ExecuteNonQuery();
con.Close();
//上传后立即在picturebox1中显示已上传的照片
MemoryStream mms = new MemoryStream(imagebytes);
Bitmap bmpt2 = new Bitmap(mms);
pictureBox1.Image = bmpt2;
}
}
从数据库中把图片读出来。
private void button1_Click(object sender, EventArgs e)
{
byte[] imagebytes = null;
//打开数据库
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=***;database=***");
con.Open();
SqlCommand com = new SqlCommand("select top 1* from ****", con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
// imagebytes = (byte[])dr.GetValue(1);//如果用这句,遇到没有照片的情况会出错。
if (dr.GetValue(0) != DBNull.Value)//先判断是否有照片信息,如果没有,则不显示。
{
imagebytes = (byte[])dr.GetValue(0);
MemoryStream mms = new MemoryStream(imagebytes);
Bitmap bmpt2 = new Bitmap(mms);
pictureBox1.Image = bmpt2;
}
else
{
pictureBox1.Image = null;
}
}
dr.Close();
com.Clone();
con.Close();
}
本实例主要介绍如何只允许输入指定图片格式。用OpenFileDialog控件打开图片文件,只要将OpenFileDialog控件的Filter属性指定为特定的图片格式即可。例如,打开.bmp文件的图片,主要代码如下:
this.openFileDialog1.Filter = "bmp文件(*.bmp)|*.bmp";
在用pictureBox控件输入图片时,要想统一图片大小,只需把控件的SizeMode属性值设为StretchImage即可,StretchImage值表示图像的大小将调整为控件的大小。这样,图片的大小就统一了。
转自:http://hi.baidu.com/ysusoft/blog/item/f9c86acc9fde8d1501e92847.html
- 在数据库中保存和读取图片
- 在oracle数据库中保存和显示图片
- 在页面中保存和读取cookie
- 在SQLServer中保存和输出图片
- 在中保存和输出图片
- asp中读取数据库中保存的图片存入硬盘
- 如何在网页上显示数据库中保存的图片
- 结构体在文件中保存和读取
- 在数据库中保存会话
- 在数据库中保存数据
- 在SQL Server中保存和输出图片
- 在SQL Server中保存和输出图片
- 如何在SQLServer中保存和输出图片
- 在SQL Server中保存和输出图片
- 在SQL Server中保存和输出图片[转]
- 在excel中保存Microsoft Query查询和数据库密码
- jdbc向数据库中保存图片
- 在手机相册中保存图片失败
- 语义层数据库维护之数据表维护
- 2009年7月自考马克思主义基本原理概论模拟试题及答案(一)
- 从OpenSolaris说起
- 关于堆和栈的搜集
- 当SqlDataSource 遇上参数是uniqueidentifier
- 在数据库中保存和读取图片
- Word中自定义的自动编号在重新编号时缩进问题的解决
- 错误:jsp.error.beans.property.conversion
- 嵌入式开发—C语言面试题
- Linux(ubuntu)下编程方面-杂项
- 思成出品: 数据结构视频课程01-1
- 如何明确故事内容
- C语言面试题集锦
- Popup a Browser Window