string ConnectionString ="server=.;uid=sa;pwd=;database=system";     
SqlConnection conn=new SqlConnection(ConnectionString);
string sql="select * from table1";
SqlCommand command = new SqlCommand(sql,conn);
conn.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
              byte[] image =(byte[])dr[0];   
  MemoryStream ms = new MemoryStream(image);  
  this.pictureBox1.Image = Image.FromStream(ms);
}
else

MessageBox.Show("没有成功读入数据!") ;
   
}

解决方案 »

  1.   

    是运行阶段还是编译阶段报错?using System.IO加上了吗?
      

  2.   

    未处理的“System.ArgumentException”类型的异常出现在 system.drawing.dll 中。其他信息: 使用了无效参数。
      

  3.   


    加了 using System.IO
    就是报      “使用了无效参数。”
      

  4.   

    断点调试一下,看看你的image 是否为空啊?
      

  5.   

    这样试下看看:byte[] image =(byte[])dr[0]; 
    MemoryStream myStream = new MemoryStream(image, true);
    myStream.Write(image, 0, image.Length);
    this.pictureBox1.Image = Image.FromStream(myStream);也不确定..
      

  6.   

    感觉没什么问题的...另外检查一下,image是不是第一个字段?
      

  7.   

    create table table1
    (
    aa image,
    bb varchar(200)
    )你看是不是第一个呀。。这个问题好久还没解决呀。。哭呀
      

  8.   

    try..SqlDataReader dr = command.ExecuteReader(CommandBehavior.SequentialAccess);
      

  9.   

    以前做过类似的,不过没有显示在PictureBox中,而是存储在了硬盘中..
      

  10.   

    应该不会吧...你断点调试一下,看哪句代码出现的问题??MemoryStream ms = new MemoryStream(image);
    this.pictureBox1.Image = Image.FromStream(ms);前一句还是后一句啊?到底是哪个参数无效?
      

  11.   

    是MS这个参数呀。。
    我存的时候调用 byte[] photo = GetPhoto(this.openFileDialog1.FileName);  这个函数啊。。帮我看看呀有没有错呀
    private void button2_Click(object sender, System.EventArgs e)
    {
    //this.pictureBox1.Image=null;
    try
    {
    byte[] photo = GetPhoto(this.openFileDialog1.FileName);
    SqlConnection nwindConn = new SqlConnection("server=.;database=System;uid=sa;pwd=;"); SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values('"+photo+"')", nwindConn);  nwindConn.Open(); addEmp.ExecuteNonQuery(); nwindConn.Close();
    }
    catch(SqlException x)
    {
    MessageBox.Show(x.Message);
    } } public static byte[] GetPhoto(string filePath)
    {
    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    byte[] photo = br.ReadBytes((int)fs.Length);
    br.Close();
    fs.Close();
    return photo;
    }
      

  12.   

    恩恩 好 的。。谢谢了
    private void 新增学生表_Load(object sender, System.EventArgs e)
    {

    } private void button1_Click(object sender, System.EventArgs e) //浏览要添加的图片
    {
    if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
    {
    try
    {
    this.pictureBox1.Image=Image.FromFile(this.openFileDialog1.FileName);
    }
    catch(Exception x)
    {
    MessageBox.Show("图片的像素过大"+x.Message);
    }   
    }
    } private void button2_Click(object sender, System.EventArgs e)  //存储SQL图片
    {
    //this.pictureBox1.Image=null;
    try
    {
    byte[] photo = GetPhoto(this.openFileDialog1.FileName);
    SqlConnection nwindConn = new SqlConnection("server=.;database=System;uid=sa;pwd=;"); SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values('"+photo+"')", nwindConn);  nwindConn.Open(); addEmp.ExecuteNonQuery(); nwindConn.Close();
    }
    catch(SqlException x)
    {
    MessageBox.Show(x.Message);
    } } public static byte[] GetPhoto(string filePath)  //函数
    {
    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    byte[] photo = br.ReadBytes((int)fs.Length);
    br.Close();
    fs.Close();
    return photo;
    } private void button3_Click(object sender, System.EventArgs e) //读取 SQL图片
    {
    try
    {
    string ConnectionString ="server=.;uid=sa;pwd=;database=system";     
    SqlConnection conn=new SqlConnection(ConnectionString);
    string sql="select aa from table1 where bb='keling'";
    SqlCommand command = new SqlCommand(sql,conn);
    conn.Open();
    SqlDataReader dr = command.ExecuteReader(CommandBehavior.SequentialAccess);
    if(dr.Read())
    {
        //byte[] image =(byte[])dr[0]; 
    //MemoryStream ms = new MemoryStream(((byte[])dr["aa"]));  
    //this.pictureBox1.Image = Image.FromStream(ms,true);
    byte[] image =(byte[])dr["aa"]; 
    MemoryStream myStream = new MemoryStream(image, true);
    myStream.Write(image, 0, image.Length);
    this.pictureBox1.Image = Image.FromStream(myStream);

    }
    else

    MessageBox.Show("没有成功读入数据!") ;
       
    }
            
    conn.Close();
    }
    catch(Exception x)
    {
    MessageBox.Show(x.Message);
    }
       
            
    }
      

  13.   

    你改成用参数的形式试试:            SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values(@Image)", nwindConn);
                addEmp.Parameters.Add("@Image", pthoto);
      

  14.   

    原因很简单,就是因为你插入时有问题..SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values('"+photo+"')", nwindConn); 既然photo是二进制数组,你怎么能用单引号将photo引起来呢?
      

  15.   

    我改成用参数形式就OK了.......
    byte[] photo = GetPhoto(this.textBox1.Text);
                    SqlConnection nwindConn = new SqlConnection("server=.;database=test;uid=sa;pwd=0421");                SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values(@Image)", nwindConn);
                    addEmp.Parameters.AddWithValue("@Image", photo);                nwindConn.Open();                addEmp.ExecuteNonQuery();......我的是Vs2005,所以用的AddWithValue,你2003可以用Add方法...试试看吧...
      

  16.   

    为什么要用
                   SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values(@Image)", nwindConn);
                    addEmp.Parameters.AddWithValue("@Image", photo);
    这个呀
      

  17.   

    TO:
    SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values(@Image)", nwindConn);
                    addEmp.Parameters.AddWithValue("@Image", photo);至少不能像你那样用,因为它是字节数组,所以不能用单引号引起来...
      

  18.   

    哦..知道了。谢谢了。。
    如果我用   INSERT INTO table1(aa) Values(""+photo+"")。这样可以吗
      

  19.   


    while (sqlDataReader.Read())
    {
    //这里 改这个地方( byte[] image =(byte[])dr[0]; )
    MemoryStream memoryStream=new MemoryStream(sqlDataReader.GetSqlBinary(1).Value);Image listViewImage=Image.FromStream (memoryStream);
    imageList.Images.Add(listViewImage); 
    }
    this.listView1.SmallImageList=imageList;