如何把数据库中的图象加载到picturebox

skydaxia 2003-08-21 11:30:09
如何把数据库中的图象加载到picturebox
用c#
...全文
52 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
offer 2003-10-25
  • 打赏
  • 举报
回复
ddd
skydaxia 2003-08-22
  • 打赏
  • 举报
回复
cmd.CommandText="select 照片 from employees where id=1";
出错:
使用了无效参数
panyee 2003-08-22
  • 打赏
  • 举报
回复
你存时就错了, 应该用参数的方法

imc.CommandText=@"insert into employees ('照片') values (@imagedata)";
imc.Parameters.Add("@imagedata", SqlDbType.Image, imagedata.Length, "imagedata").Value = imagedata;


skydaxia 2003-08-22
  • 打赏
  • 举报
回复
会不会有错啊
skydaxia 2003-08-22
  • 打赏
  • 举报
回复
用System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
也一样,
看看我的储存图象
private void imagestore()
{
///储存图象
SqlConnection cn=new SqlConnection(sqlstr);
cn.Open();
try
{

byte[] imagedata=loadimg();

MessageBox.Show(imagedata.ToString());

SqlCommand imc=cn.CreateCommand();

imc.CommandText=@"insert into employees ('照片') values (imagedata)";

imc.ExecuteNonQuery();

}

catch(Exception x)

{MessageBox.Show(x.Message.ToString()+"dfgdf");}

finally

{

cn.Close();

}

}

byte[] loadimg()
{

try

//MessageBox.Show(Connectstr);

FileStream fs=new FileStream(Connectstr,FileMode.Open );

BinaryReader br=new BinaryReader(fs);
byte[] imagebyte=br.ReadBytes(maximagesize);
fs.Close();
return imagebyte;

}
catch(Exception x)
{MessageBox.Show("erwrw"+x.Message.ToString());return null;}

}

}
panyee 2003-08-22
  • 打赏
  • 举报
回复
哦? 哪行报的错?

还是Image img = Image.FromStream(sm);????

是不是图片的内容本身就是错的?
skydaxia 2003-08-22
  • 打赏
  • 举报
回复
SqlConnection cn=new SqlConnection(sqlstr);
cn.Open();
SqlCommand cmd=cn.CreateCommand();
cmd.CommandText="select 照片 from employees ";
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
byte[] imagedata=(byte[])dr["照片"];
MemoryStream sm = new MemoryStream();
sm.Write(imagedata, 0, imagedata.Length);
Image img = Image.FromStream(sm);
pictureBox1.Image = img;
}

cn.Close();
出错信息:指定转换无效
skydaxia 2003-08-22
  • 打赏
  • 举报
回复
我试试
panyee 2003-08-22
  • 打赏
  • 举报
回复
可能是这错了,
不要把它读到dataset中, 你用SqlDataReader来读
byte[] imagedata = (byte[])read["照片"];

skydaxia 2003-08-22
  • 打赏
  • 举报
回复
很奇怪啊
储存是对的啊
skydaxia 2003-08-22
  • 打赏
  • 举报
回复
真是太感谢了
不过还是一样
使用了无效参数

panyee 2003-08-22
  • 打赏
  • 举报
回复
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
picturebox1.Image = img;
skydaxia 2003-08-22
  • 打赏
  • 举报
回复
我决的我的储存没关系的啊
skydaxia 2003-08-22
  • 打赏
  • 举报
回复
还是不行
skydaxia 2003-08-22
  • 打赏
  • 举报
回复
Image
panyee 2003-08-22
  • 打赏
  • 举报
回复
你数据库中图片的字段类型是什么?
skydaxia 2003-08-22
  • 打赏
  • 举报
回复
那是一样的
因为: SqlCommand imc=cn.CreateCommand();
panyee 2003-08-22
  • 打赏
  • 举报
回复
cmd.CommandText="select 照片 from employees where id=1";
这句出错我也想不通; :(

你为什么不用

SqlCommand cmd = new SqlCommand("select 照片 from employees where id=1", cn);
skydaxia 2003-08-22
  • 打赏
  • 举报
回复
但是数据库中好象已经储存成功了
显示byte[] 数组
skydaxia 2003-08-21
  • 打赏
  • 举报
回复
不是对了
而是不出错了
加载更多回复(4)

110,534

社区成员

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

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

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