picturebox显示图片问题

lvquanming 2008-05-08 10:18:07
string strcon = "Server='127.0.0.1';Initial Catalog='Library';user id='';pwd=''";
SqlConnection myconn = new SqlConnection(strcon);

string strsql = "select photo from UserInfo";
SqlCommand mycommand = new SqlCommand(strsql, myconn);
myconn.Open();
SqlDataReader dr = mycommand.ExecuteReader();
if (dr.Read())
{
byte[] image_bytes = (byte[])dr["photo"];
MemoryStream ms = new MemoryStream(image_bytes);
Image image = Image.FromStream(ms);

pictureBox1.Image = image;
}
提示ms参数无效,请问各位是怎么回事,谢谢!
...全文
90 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvquanming 2008-05-09
  • 打赏
  • 举报
回复
我是直接在SQL SERVER的查询分析器中存入的图片 insert into values('图片的路径')
superaremeng 2008-05-09
  • 打赏
  • 举报
回复
可能你存图片时的代码有问题

如果的的代码是:SqlCommand cmd = new SqlCommand("insert into MyPictures values('"+imgData+"',null)", conn);

要改成:SqlCommand cmd = new SqlCommand("insert into MyPictures values(@Image,null)", conn);
SqlParameter sp = new SqlParameter("@Image", SqlDbType.Image);
sp.Value = imgData;
cmd.Parameters.Add(sp);
lvquanming 2008-05-09
  • 打赏
  • 举报
回复
这样还是没用,我把image该成了nvarchar
lvquanming 2008-05-09
  • 打赏
  • 举报
回复
谢谢,以前没试过这个,我去改下看看
Choi57671452 2008-05-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lvquanming 的回复:]
我是直接在SQL SERVER的查询分析器中存入的图片 insert into values('图片的路径')
[/Quote]

晕,你存的是路径,不是图片。可你的代码却是在读取图片。
insert into Table("photo") vaues(picturebox.Image)
superaremeng 2008-05-09
  • 打赏
  • 举报
回复
如果你存的是路径,数据库中字段类型应为varchar(50),读的时候应
if (dr.Read())
{
string imagepath = dr["photo"];
Image image = Image.FromFile(imagepath);

pictureBox1.Image = image;
}

照你的读法,数据库中字段为image类型,这种类型不能用查询分析器存入
Choi57671452 2008-05-08
  • 打赏
  • 举报
回复

/// <summary>
/// 将一组一维的字节数组转为图像。
/// </summary>
/// <exception cref="ArgumentNullException">Bytes 为 null 引用(在 Visual Basic 中为 Nothing)。</exception>
/// <exception cref="ArgumentException">Bytes 不包含图像数据。</exception>
/// <param name="Bytes">要转为图像的一组一维的字节数组。</param>
/// <returns>转换后的图像对象。</returns>
static public Bitmap BytesToBitmap(byte[] Bytes)
{
MemoryStream stream = null;
try
{
stream = new MemoryStream(Bytes);
return new Bitmap(stream);
}
catch (ArgumentNullException ex)
{
throw ex;
}
catch (ArgumentException ex)
{
throw ex;
}
finally
{
stream.Close();
}
}


可以转的,如果转不了,说明查到的是空的图片。
注:异常是从类库往外抛,你可以去掉。
lvquanming 2008-05-08
  • 打赏
  • 举报
回复
上面的应用程序里的,不是WEB的
lvquanming 2008-05-08
  • 打赏
  • 举报
回复
楼上的不行,有错误
周公 2008-05-08
  • 打赏
  • 举报
回复
两种办法:

if (dr.Read())
{
byte[] image_bytes = (byte[])dr["photo"];
MemoryStream ms = new MemoryStream(image_bytes);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
pictureBox1.Image = image;
}


你说到的参数无效的问题,我想是因为在System.Web.UI.WebControls下有一个Image(Image服务器端控件),在System.Drawing下也有一个Image(图像抽象类),这二者冲突的问题。


byte[] Bytes = (byte[])dr["photo"];
MemoryStream stream = new MemoryStream(Bytes);
pictureBox1.Image =new Bitmap(stream);
Choi57671452 2008-05-08
  • 打赏
  • 举报
回复

byte[] Bytes = (byte[])dr["photo"];
MemoryStream stream = new MemoryStream(Bytes);
pictureBox1.Image = Bitmap(stream);

110,534

社区成员

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

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

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