方式一:

数据库用的是sql 2008,数据表中存放的是图片的二进制数据,现在把图片以一种图片格式(如.jpg)导出,然后存放于指定的文件夹中,实现方式如下:

byte[] bytimg = (byte[])mydal.dbhelpersql.query("select f_photo from mytable where id=1").tables[0].rows[0][0];
if (bytimg != null)
{
 memorystream ms = new memorystream(bytimg);
 image img = image.fromstream(ms);
 img.save("d:\\me.jpg");
}

方式二:

是windowform程序,数据库已经建好,图像以二进制形式存放在数据库的image表中,我想把符合查询条件的图像(大量)从数据库中读出,显示在form窗体上的一个控件(listview或imagelist还是picturebox?这个不知道那个合适),并保存到选择(或新建)的一个文件夹中

sqldataadapter da = new sqldataadapter("select * from newpicture", conn);//数据库连接,修改一下数据库的操作。
dataset ds = new dataset();
da.fill(ds, "pic");//将符合条件的选项保存在数据集的pic表里
  
string picdotname;
string picfilename;
int piclength;
int i;
//添加新列
datacolumn newcolumn = ds.tables["pic"].columns.add("pic_url", typeof(string));//给pic表添加新的一列pic_url,保存你的新写出的图片路径
for (i = 0; i < convert.toint16(ds.tables["pic"].rows.count); i++)
{
 picdotname = ds.tables["pic"].rows[i]["pic_dot"].tostring();//图片的拓展名,你数据库要有这一列,如jpg
 piclength = convert.toint32(ds.tables["pic"].rows[i]["pic_length"]);//数据流的长度
 picfilename = server.mappath("新建的文件夹名/") + "添加图片名"+ "." + picdotname;
 filestream fs = new filestream(picfilename, filemode.create, fileaccess.write);
 byte[] piccontent = new byte[piclength];
 piccontent = (byte[])ds.tables["pic"].rows[i]["pic_content"];
 fs.write(piccontent, 0, piclength);
 fs.close();//读出数据流写成图片
 //最后把表绑定到控件上。
 ds.tables["pic"].rows[i]["pic_url"] = "temp/temp" + i.tostring() + "." + picdotname;//意思给表pic的第i行,pic_url列里添加文件的路径值。
}
//数据源 = ds.tables["pic"];//数据绑定

大体是这样吧,里面表名列名很多细节你按你的表修改吧!

以上就是c#从数据库读取图片并保存的两种方法的详细内容,更多关于c# 读取图片并保存的资料请关注www.887551.com其它相关文章!