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参数无效,请问各位是怎么回事,谢谢!
解决方案 »
- 用C#开发分布式数据库应用winform程序用到的技术和知识点?
- .net中,前台提交了信息,后台怎么实现声音提醒
- C# picturebox控件数组选择问题
- 属性转 字符串的 问题
- 高分求助 DXperience控件自带的Demo Center中的XtraGird Stuite实例中的XtraGird Tutorials中各个例子的所介绍的功能
- API在游戏中点击鼠标失灵了?
- set temp=server.createobject("scripting.dictionary")
- MSDN WEBCAST 现在如何下载讲过的课程?
- 打开窗口后为什么没有初始化新窗口中的树形目录?帮看看
- C# 判断FTP某个文件夹里是否为空文件夹
- 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)