SQL Server中的Image类型的字段如何转换到C#中Image类型变量?

小流星子 2003-08-25 01:03:15
我想从数据库中把一个Image类型的字段取得后,填入到一个Image变量中,有什么办法?
谢谢
...全文
67 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
小流星子 2003-08-25
  • 打赏
  • 举报
回复
表中的数据不可能错的,是系统自带的NorthWind数据库。
我的代码如下:
byte [] byt=(byte[]) dataset1.Tables[0].Rows[0]["Picture"];
System.IO.MemoryStream imagestream=new System.IO.MemoryStream();
imagestream.Write(byt,0,byt.Length);
System.Drawing.Image sourceimage=System.Drawing.Image.FromStream(imagestream);//执行到这里出错,提示参数无效。
panyee 2003-08-25
  • 打赏
  • 举报
回复
我不知道你的表中数据是否正确, 你用下面的代码

SqlConnection conn = new SqlConnection("server=pany;database=pany;uid=sa;pwd=123");
SqlCommand cmd = new SqlCommand("select * from t_imgs where id=1", conn);
conn.Open();
SqlDataReader read = cmd.ExecuteReader();
read.Read();
byte[] bytes = (byte[])read["imgData"];
read.Close();
conn.Close();
MemoryStream ms = new MemoryStream();
ms.Write(bytes, 0, bytes.Length);
Image img = System.Drawing.Image.FromStream(ms);
小流星子 2003-08-25
  • 打赏
  • 举报
回复
为什么会这样呢?
小流星子 2003-08-25
  • 打赏
  • 举报
回复
Image img = System.Drawing.Image.FromStream(ms);
执行这句时提示,使用了无效参数。
小流星子 2003-08-25
  • 打赏
  • 举报
回复
谢谢,不知为什么 我试过,提示
byte[] bytes = (byte[])read["imgData"];
“指定的转换无效”
panyee 2003-08-25
  • 打赏
  • 举报
回复
byte[] bytes = (byte[])read["imgData"];
MemoryStream ms = new MemoryStream();
ms.Write(bytes, 0, bytes.Length);
Image img = System.Drawing.Image.FromStream(ms);

110,538

社区成员

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

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

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