如何显示从datagridview到picturebox的图像? [英] How to display image from datagridview to picturebox?

查看:66
本文介绍了如何显示从datagridview到picturebox的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助才能将数据从datagridview显示到图片框,有人可以帮助我吗?我对此很陌生.也是这个网站.

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.

我用它来保存图像

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

以下是显示datagridview

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

该图像显示在此列的datagridview中:

The image is shown in the datagridview in this column:

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

我尝试过:

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

并出现以下错误:

在System.Drawing.dll中发生了类型为'System.IO.FileNotFoundException'的未处理异常

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

其他信息:System.Byte []

Additional information: System.Byte[]

推荐答案

如果该列中有图像的文件路径,请使用

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

这篇关于如何显示从datagridview到picturebox的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆