How to display image from datagridview to picturebox?

妖精的绣舞 提交于 2019-12-11 16:31:59

问题


I need some help to display images from my datagridview to my picturebox, can someone please help me? I'm very new to this. To this site as well.

I've used this to save the images

    private void button1_Click(object sender, EventArgs e)
    {
        byte[] imageBt = null;
        FileStream fstream = new FileStream(this.afbeelding_txt.Text, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fstream);
        imageBt = br.ReadBytes((int)fstream.Length);

        string constring = "datasource=localhost;port=3306;username=username;password=password";
        string Query = "INSERT INTO project.auto (kenteken, merk, type, kleur, deuren, prijscategorie, afbeelding) VALUES('" + this.kenteken_txt.Text + "','" + this.merk_txt.Text + "','" + this.type_txt.Text + "','" + this.kleur_txt.Text + "','" + this.deuren_txt.Text + "','" + this.prijscategorie_txt.Text + "',@IMG) ;";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;
        try
        {
            conDataBase.Open();

            cmdDataBase.Parameters.Add(new MySqlParameter("@IMG", imageBt));

            myReader = cmdDataBase.ExecuteReader();
            MessageBox.Show("Opgeslagen");
            while (myReader.Read())
            {

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        load_table();
    }

And the following is to show the datagridview

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if(e.RowIndex >= 0)
        {
            DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];

            kenteken_txt.Text = row.Cells["kenteken"].Value.ToString();
            merk_txt.Text = row.Cells["merk"].Value.ToString();
            type_txt.Text = row.Cells["type"].Value.ToString();
            kleur_txt.Text = row.Cells["kleur"].Value.ToString();
            deuren_txt.Text = row.Cells["deuren"].Value.ToString();
            prijscategorie_txt.Text = row.Cells["prijscategorie"].Value.ToString();
            afbeelding_txt.Text = row.Cells["afbeelding"].Value.ToString();
        }
    }

Besides this code the picturebox isn't mentioned.

    private void button6_Click(object sender, EventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Filter = "PNG Files(*.png)|*.png|JPG Files(*.jpg)|*.jpg|All Files(*.*)|*.*";
        dlg.Title = "Selecteer auto afbeelding.";
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            string picPath = dlg.FileName.ToString();
            afbeelding_txt.Text = picPath;
            pictureBox1.ImageLocation = picPath;
        }
    }

The image is shown in the datagridview in this column:

    afbeelding_txt.Text = row.Cells["afbeelding"].Value.ToString();

I tried :

    pictureBox1.Image = Image.FromFile(row.Cells["afbeelding"].Value.ToString());

And got the following error :

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll

Additional information: System.Byte[]


回答1:


If in the column there is the file path of the image, use the Image.FromFile method:

pictureBox1.Image = Image.FromFile(row.Cells["afbeelding"].Value.ToString());

Else, if in the column there is directly the image value you can use the FromStream method as described here, in your case:

var data = (Byte[])(row.Cells["afbeelding"].Value);
var stream = new MemoryStream(data);
pictureBox1.Image= Image.FromStream(stream);


来源:https://stackoverflow.com/questions/20533691/how-to-display-image-from-datagridview-to-picturebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!