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参数无效,请问各位是怎么回事,谢谢!
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参数无效,请问各位是怎么回事,谢谢!
解决方案 »
- 我想从ACCESS中读取图片的路径,然后在WINFORM中用PICTUREBOX来显示,怎么做
- C# 用SQLDMO备份还原数据库问题
- 嵌入webbrowser控件的窗体在最小化时弹出“错误”对话框!
- 正则表达式:如何在一个多行的字符串中匹配以字符 ' 开始,以 ! 结束的所有字符串?
- 是CollectionBase怪呢?还是我菜呢???????
- 强人来指点一下NickLee.Web.UI.TreeView这个树形控件.
- 关于短信自动接收 委托和事件
- Access数据库的日期/时间字段存入字段为空时不能存入
- jdbc 连接mysql,muti connection 一般用什么方式比较好
- 我想自已编一个在电脑中查询文件的程序用C#,请问谁有这样的例子,或谁会,帮我一下
- sql无法更新数据库
- 悬赏100元人民币求一个webservice的调用代码,对高手来说就几句代码!
byte[] Bytes = (byte[])dr["photo"];
MemoryStream stream = new MemoryStream(Bytes);
pictureBox1.Image = Bitmap(stream);
{
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);
/// <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();
}
}
可以转的,如果转不了,说明查到的是空的图片。
注:异常是从类库往外抛,你可以去掉。
SqlParameter sp = new SqlParameter("@Image", SqlDbType.Image);
sp.Value = imgData;
cmd.Parameters.Add(sp);
if (dr.Read())
{
string imagepath = dr["photo"];
Image image = Image.FromFile(imagepath);
pictureBox1.Image = image;
} 照你的读法,数据库中字段为image类型,这种类型不能用查询分析器存入
晕,你存的是路径,不是图片。可你的代码却是在读取图片。
insert into Table("photo") vaues(picturebox.Image)