从数据库读出图片显示到datagridview
  数据库里面就一个字段Image  类型image   存放着图片的信息
 怎么取出来放到datagridview 的picturebox 里面??? 或者直接在datagridview显示

解决方案 »

  1.   


           string st = "select BookImage from Books";
                DataTable dt = DB.Select(st);
                byte[] imgbt = (byte[])dt.Rows[0][0];
                if (imgbt.Length != 0)
                {
                    MemoryStream imgms = new MemoryStream(imgbt);
                    Bitmap images = new Bitmap(imgms);                                   //这里提示参数类型不正确 
                    Column1.Image = images;                              //Column1为我picturebox的   字段
                } DB连接数据库的类。    帮忙一下
      

  2.   

    private void dataGridview1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)  
    {  
      if (dataGridview1.Columns[e.ColumnIndex].Name.Equals("Image"))  
      {  
      string path = e.Value.ToString();  
      e.Value = GetImage(path);  
      }  
    }  
    public System.Drawing.Image GetImage(string path)  
    {  
      System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);  
      System.Drawing.Image result = System.Drawing.Image.FromStream(fs);    fs.Close();    return result;  }  
    使用DataGridViewImageColumn  
    DataGridViewImageColumn column = new DataGridViewImageColumn();  
    dataGridView1.Columns.Add(column);  
    column.HeaderText = "图片";  
    column.Image = System.Drawing.Image.FromFile("路径");  ((DataGridViewImageCell)this.dataGridView1.Rows[e.RowIndex].Cells[ "图片列"]).Value = "路经 "
      

  3.   

     dataGridView1.DataSource =dt;
     dataGridView1.Columns["uwork"].Visible = false;    //隐藏该列
      DataGridViewImageColumn colphoto = new DataGridViewImageColumn();
               colphoto.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;     //调整列宽,使其足够显示出标题
               colphoto.HeaderText = "图片";
               colphoto.Name = "photo";
    //一下为添加计算字段函数
               dataGridView1.CellValueNeeded += new DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
               dataGridView1.Columns.Add(colphoto);关于计算字段的函数请参考:
    http://www.cnblogs.com/liminzhang/articles/592955.html
      

  4.   

             if (imgbt.Length != 0)
                {
                    MemoryStream imgms = new MemoryStream(imgbt,true);
    imgms .Write(imgbt, 0, imgbt.Length);                                 //这里提示参数类型不正确 
                    Column1.Image = new Bitmap(imgms);//Column1为我picturebox的   字段                            imgms.Close();
                }
      

  5.   

    也不行a !!!  也是提示参数无效  imgms 是null ...所以参数无效吧??
             byte[] imgbt = (byte[])dt.Rows[0][0];
                           是不是这句出问题啦。。         
      

  6.   

       pictureBox1.Image = null;
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = new SqlConnection("Data Source=10.192.35.20;User ID=sa;password=sa123;initial catalog=ems;Integrated Security=no;");
                cmd.CommandText = "select top 1 * from Tbl_Test_Img";
                cmd.CommandType = CommandType.Text;
                if (cmd.Connection.State != ConnectionState.Open)
                {
                    cmd.Connection.Open();
                }
                SqlDataReader dr = cmd.ExecuteReader();            dr.Read();
                MemoryStream ms = new MemoryStream((byte[])dr["img"]);
                Image img = Image.FromStream(ms);
                pictureBox1.Image = img;