0

How to display Image from dataGridView by Selected the Row and show the image to pictureBox in C# WindowsForm

Problem: Cannot retrieve image from datagridView to pictureBox.

I Tried the Code Below how to display image on picture box by Select from the dataGridView which has been save on a database as byte. But Errors Occurs as that System.String cannot cast to system.Byte and When I Tried the other code it Says Invalid Argument Here is My Code Below:

  1. DataGridview using Mouse Click Event to display the values and image when select data on DataGridView
  2. Show how I Browse Picture from the system and upload to pictureBox
  3. Show how I save image from Picture Box to Database.

I hope You Get my problem and help me solve this issue.

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);
}
    }

*****My Code to upload Image to pictureBox*****

Code to Browse Image

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);
        }

******My Code to Save Image to database*****

Code to Save Image to DataBase

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);
        }
  • There are many things wrong in that code. You should research DataAdapters, DataTables and DBCommand Parameters (as well as SQL injection). These are easier, cleaner ways to do all that. Also please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Jan 14 '18 at 20:20
  • If `Cells[10].Value` is an image... don't use `ToString()` on it. Just cast it to `Image` class and you got your image. – Nyerguds Feb 11 '18 at 10:57
  • thank you sir for your time i appreciate it. – Marshillong Feb 20 '18 at 13:02

0 Answers0