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参数无效,请问各位是怎么回事,谢谢! 

解决方案 »

  1.   


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

  2.   

    两种办法: 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);
      

  3.   

    上面的应用程序里的,不是WEB的
      

  4.   


            /// <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();
                }
            }
    可以转的,如果转不了,说明查到的是空的图片。
    注:异常是从类库往外抛,你可以去掉。
      

  5.   

    可能你存图片时的代码有问题如果的的代码是: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); 
      

  6.   

    我是直接在SQL SERVER的查询分析器中存入的图片 insert into values('图片的路径')
      

  7.   

    如果你存的是路径,数据库中字段类型应为varchar(50),读的时候应
    if (dr.Read()) 
                { 
                    string imagepath = dr["photo"]; 
                    Image image = Image.FromFile(imagepath); 
                     
                    pictureBox1.Image = image; 
                } 照你的读法,数据库中字段为image类型,这种类型不能用查询分析器存入
      

  8.   


    晕,你存的是路径,不是图片。可你的代码却是在读取图片。
    insert into Table("photo") vaues(picturebox.Image)
      

  9.   

    这样还是没用,我把image该成了nvarchar