[英]How to display image from datagridview to picturebox?
我需要一些帮助来显示从我的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 []
如果列中有图像的文件路径,请使用Image.FromFile方法 :
pictureBox1.Image = Image.FromFile(row.Cells["afbeelding"].Value.ToString());
否则,如果在列中直接存在图像值,则可以使用此处所述的FromStream方法,在您的情况下:
var data = (Byte[])(row.Cells["afbeelding"].Value);
var stream = new MemoryStream(data);
pictureBox1.Image= Image.FromStream(stream);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.