我用stream都给报参数无效错误还有必须用dataset读数据库里的值,这样的Byte[] data =((byte[])dt.Rows[row]["photo"]);
让其显示在pictureBox里,总报错啊,急死我了,谁又没有好的办法吖??

解决方案 »

  1.   


            private void toolStripButton2_CheckedChanged(object sender, EventArgs e)
            {
                SqlConnection conn = new SqlConnection(@"data source=.;uid=sa;pwd=;database=master");
                conn.Open();
                SqlCommand cmd = new SqlCommand("select image1 from image", conn);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                        MemoryStream buf = new MemoryStream((byte[])reader[0]);
                        Image image = Image.FromStream(buf,true);
                        this.pictureBox1.Image = image;
                    }
                }
            }
      

  2.   

    我是这样做的,在数据库里保存图片的路径,把路径读到dataset里,在显示到picturebox
    不知道对楼主有没有启发
      

  3.   

    使用ImageConverter 来转换就可以了:Byte[] data =((byte[])dt.Rows[row]["photo"]); if (bts != null)
    {
         ImageConverter imc = new ImageConverter();
         Image _img = imc.ConvertFrom(null, CultureInfo.CurrentCulture, data) as Image;
         if (_img != null)
         {
              return _img;
         }
    }
      

  4.   

    byte[] btImage = (byte[])dsImage.Tables[0].Rows[0]["Photo"];
    这样出来只是 流 格式 , 所以你得转化为 image 类型的 
    Image img = Image.FromStream(btImage,true);
    Page.picturBox1.Image=img;
                
      

  5.   

    3楼的说法我照做了,可还是报参数无效错误啊,在 Image _img = imc.ConvertFrom(null, CultureInfo.CurrentCulture, data) as Image;这样行5楼的也有错,帮我想想吧
      

  6.   

     this.txtpapernum.Text = dt.Rows[row]["userpapernum"].ToString();
                    this.txtaddress.Text = dt.Rows[row]["useraddress"].ToString();
                    this.txthobby.Text = dt.Rows[row]["userhobby"].ToString();
                    Byte[] data =((byte[])dt.Rows[row]["photo"]);
                   
                        ImageConverter imc = new ImageConverter();
                        Image _img = imc.ConvertFrom(null, CultureInfo.CurrentCulture, data) as Image;
                        if (_img != null)
                        {
                            this.pictureBox1.Image = _img;
                        }
                    Image _img = imc.ConvertFrom(null, CultureInfo.CurrentCulture, data) as Image; 这样一行报错了
    目前的情况我不能用SqlDataReader ,你帮我看看为什么老报那个参数无效呢、??
      

  7.   

    Image _img = imc.ConvertFrom(data) as Image;
    直接一个参数不行吗? 
      

  8.   

    if (dtZG.Rows[0]["相片"].ToString() != "")
                                {
                                    byte[] bytes = (byte[])dtZG.Rows[0]["相片"];
                                    MemoryStream memStream = new MemoryStream(bytes);
                                    try
                                    {
                                        Bitmap myImage = new Bitmap(memStream);
                                        this.picPhoto.BackgroundImage = myImage;
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show("获取照片发生异常:\r\n" + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                }
      

  9.   

    数据库中存储图片(二进制格式)
    看到好多类似的帖子..要么求助..要么不说..本人经历过了..虽然方法也是问的别人..但本人不敢独享,,,拿出来和大家分享一下...有点粗糙,,大家谅解..首先..定义一个函数..将图片转化为二进制码
    //定义将图片转化为长二进制代码的函数getphoto()
    public Byte[] getphoto(string photopath)
    {
    string str = photopath;
    FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read);
    Byte[] bytBLOBData = new Byte[file.Length];
    file.Read(bytBLOBData, 0, bytBLOBData.Length);
    file.Close();
    return bytBLOBData;
    }//这是定义函数..然后..就是将转换成二进制码的图片插入数据库中..下面是简单的也是重要的sql语句..
    if (this.pictureBox1.Image != null)
    {
    sql1 = sql1 + ",Photo";
    sql2 = sql2 + ",bytBLOBData";
    Byte[] bytBLOBData = getphoto(openFileDialog1.FileName);
    cmd.Parameters.Add(new OleDbParameter("jpeg", OleDbType.Binary, bytBLOBData.Length, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, bytBLOBData));
    }接下来..是读取...string sql = "select photo from studentinfo where studentid = " + this.Tag.ToString();
    OleDbCommand cmd = new OleDbCommand(sql, connection1);
    if (Convert.DBNull != cmd.ExecuteScalar())
    pictureBox1.Image = Image.FromStream(new MemoryStream((Byte[])cmd.ExecuteScalar()));//读取长二进制为图片..//////说得很简单..旨在抛砖引玉..希望大家多多讨论...