如何通过选择行从dataGridView显示图像,并在C#WindowsForm中将图像显示到pictureBox

时间:2018-01-14 20:04:38

标签: c# image winforms datagridview picturebox

如何通过选择行从dataGridView显示图像并在C#WindowsForm中将图像显示到pictureBox

问题:无法将图像从datagridView检索到pictureBox。

我试过代码下面如何通过从dataGridView中选择来显示图片框上的图像,该数据已作为字节保存在数据库中。但错误发生,因为System.String无法转换为system.Byte和当我尝试其他代码它说无效参数这里是我的代码下面:

  1. DataGridview使用鼠标单击事件在DataGridView上选择数据时显示值和图像
  2. 显示我如何从系统浏览图片并上传到pictureBox
  3. 显示如何将图片从图片框保存到数据库。
  4. 我希望你能解决我的问题,并帮助我解决这个问题。

    private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
         {
           try
           {
                label21.Text = dataGridView1.SelectedRows[0].Cells["M_FirstName"].Value.ToString();
                textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                textBox3.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                comboBox1.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                dateTimePicker1.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                textBox6.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
                textBox7.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
                textBox8.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
                textBox9.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
                textBox10.Text = dataGridView1.SelectedRows[0].Cells[9].Value.ToString();
    
        //First try to select image from datagridview and display to picturebox
                byte[] image = Encoding.ASCII.GetBytes(dataGridView1.SelectedRows[0].Cells[10].Value.ToString());
                if (image == null)
                    pictureBox1.Image = null;
                else
                {
                        var ms= new MemoryStream(image);
                  //Error is Invalid Argument (ms)
                        pictureBox1.Image = Image.FromStream(ms);
                }
    
        //Second try to select image from datagridview and display to picturebox
        //When I tried this code below the Error shows that "*Unable to Cast Object of type 'System.String' to type 'System.Byte[]'*"
    
                var data = (Byte[])(dataGridView1.SelectedRows[0].Cells[10].Value);
                var stream = new MemoryStream(data);
                pictureBox1.Image = Image.FromStream(stream);
    
        //Both the Code Above does not work to select image from datagridview and display on picturebox
        //How can I do to make it work.
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
        }
    

    *****我的代码将图片上传到pictureBox *****

    浏览图片的代码

    try
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = "*.jpg files(*.jpg)|*.jpg| PNG files(*.png)|*.png| All Fies(*.*)|*.*";
    
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    imagelocation = dialog.FileName;
                    pictureBox1.ImageLocation = imagelocation;
                }
    
            }
            catch (Exception)
            {
                MessageBox.Show("An Error Occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    

    ******我的代码将图像保存到数据库*****

    将图像保存到数据库的代码

    try
            {
    
                if (textBox2.Text == "" || textBox3.Text == "" || comboBox1.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "" || textBox10.Text == "")
                {
                    MessageBox.Show("Please Fill all the fields !");
                }
                else
                {
    
                    byte[] image = null;
                    FileStream Streem = new FileStream(imagelocation, FileMode.Open, FileAccess.Read);
                    BinaryReader brs = new BinaryReader(Streem);
                    image = brs.ReadBytes((int)Streem.Length);
    
    
    
                    con.Open();
    
                    String qry = "Insert Into membership_tbl values('" + textBox2.Text + "','" + textBox3.Text + "','" + comboBox1.Text + "','" + dateTimePicker1.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "',@image)";
    
                    cmd = new SqlCommand(qry, con);
                    cmd.Parameters.Add(new SqlParameter("@image", image));
                    cmd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("Member Added ! ");
                    clear();
                    Membership mem = new Membership();
                    mem.Show();
                    this.Hide();
                }
    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
    

0 个答案:

没有答案
相关问题