c#怎样从数据库读取图片并保存到指定文件

2024-12-27 03:36:35
推荐回答(3个)
回答1:

SqlDataAdapter da = new SqlDataAdapter("select * from newpicture", conn);
DataSet ds = new DataSet();
da.Fill(ds, "pic");

string picdotname;
string picfilename;
int piclength;
int i;
//添加新列
DataColumn newcolumn = ds.Tables["pic"].Columns.Add("pic_url", typeof(string));
for (i = 0; i < Convert.ToInt16(ds.Tables["pic"].Rows.Count); i++)
{
picdotname = ds.Tables["pic"].Rows[i]["pic_dot"].ToString();
piclength = Convert.ToInt32(ds.Tables["pic"].Rows[i]["pic_length"]);
picfilename = Server.MapPath("temp/") + "temp" + i.ToString() + "." + 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;//相对路径,改成你自己的文件夹
}
数据源 = ds.Tables["pic"];//数据绑定
大体是这样吧,里面表名列名很多细节你按你的表修改吧!
用哪个控件都行,只要图片路径正确就能显示的

回答2:

直接显示图像,方法如下:
byte[] piccontent = (byte[])ds.Tables["pic"].Rows[i]["pic_content"];
if (piccontent.Count > 0)
{
MemoryStream ms = new MemoryStream(data);
Image image = Image.FromStream(ms, true);
this.pb_image.Image = image;
}

回答3:

this.pb_image.Image 指的是picturebox控件