关于从数据库取出image,并保存成jpeg图片。

7xin 2004-10-15 11:11:05
代码如下:
***************************************
byte[] image_bytes=(byte[])myRow1["pic_value"];
string path="/user/wangxf/"+max_id;
//保存原图片
MemoryStream ms=new MemoryStream(image_bytes);
Bitmap output=new Bitmap(ms);
output.Save(path,System.Drawing.Imaging.ImageFormat.Jpeg);
output.Dispose();
***************************************
我是循环从数据库里面取出image_bytes值,并一一生成相应的图片
程序执行的时候,只生存了第一张图片,然后提示“GDI+ 中发生一般性错误。”
这是怎一回事?》
...全文
236 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
BearRui 2004-10-16
  • 打赏
  • 举报
回复
//把图片读到pictureBox,再写入数据库

SqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("insert into fuser values ('1a','1b',@i)",conn);
byte[] ib=new Byte[60000];
FileStream fs=new FileStream(@"D:\windows temp\temp\1.jpg",FileMode.Open ,FileAccess.Read );
fs.Read(ib,0,60000);
cmd.Parameters.Add("@i",SqlDbType.Image,(int)fs.Length);
cmd.Parameters["@i"].Value=ib;
cmd.ExecuteNonQuery();
conn.Close();

-------------------------------------------------------
//从数据库读图片到picturebox

SqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream buf=new MemoryStream((byte[])reader[0]);
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image;
darkbao 2004-10-16
  • 打赏
  • 举报
回复
string path="/user/wangxf/"+max_id

改正为绝对路径,如

string path= @"e:\user\wangxf\" + max_id + ".jpg";
gbbword 2004-10-16
  • 打赏
  • 举报
回复
做这个,重点就在于,写的时候要用cmd.Parameters,要先设置参数的名字和类型。再添加值。
读的时候要用SqlDataReader 楼上说的很清楚也有代码。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧