【问题标题】:How to display image from datagridview to picturebox?如何将图像从 datagridview 显示到图片框?
【发布时间】:2013-12-12 02:03:45
【问题描述】:

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

我用它来保存图像

    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

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

除此代码外,未提及图片框。

    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中:

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

我试过了:

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

并得到以下错误:

System.Drawing.dll 中出现“System.IO.FileNotFoundException”类型的未处理异常

附加信息:System.Byte[]

【问题讨论】:

  • 你的图片框控件在哪里?
  • 如何绑定dataGridView1?有没有显示图像的列?并且您不需要使用ExecuteReader 进行插入,使用ExecuteNonQuery
  • 可以输入栏目内容吗?这是一个文件路径还是一个代表图像本身的字节数组?

标签: c# datagridview picturebox


【解决方案1】:

如果列中有图片的文件路径,使用Image.FromFile method:

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

否则,如果列中直接有图像值,您可以使用here 所述的FromStream 方法,在您的情况下:

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

【讨论】:

  • 图像在数据库中保存为 BLOB - 64 KiB。我不知道这是否会改变什么?
  • 尝试第二种解决方案
  • 非常感谢!它终于在图片框中显示图像了!还有一个问题,它只在第一行显示了一小部分图像,而第二行则更少。这和大小有关系吗?
  • 对不起,我不明白,也许你必须设置SizeMode属性:pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 2014-12-17
  • 2022-11-16
  • 2017-06-01
相关资源
最近更新 更多