如何在C#中从数据库检索图像
问题描述:
一个错误表明我有此代码的无效参数... 谁能告诉我怎么了?我应该将图像分配给clientID.
an error shows saying that I have invalid parameters for this code... can anyone tell me whats wrong? Im supposed to get the image assigned to the clientID.
private void button1_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection(mycon);
MySqlCommand cmd = new MySqlCommand("SELECT clientImage FROM client WHERE clientID='" + label2.Text + "'", conn);
conn.Open();
MySqlDataReader myReader = null;
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
byte[] imgg = (byte[])(myReader["clientImage"]);
if (imgg == null)
{
pictureBox1.Image = null;
}
else
{
MemoryStream mstream = new MemoryStream(imgg);
pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
}
}
conn.Close();
}
答
这段代码可能派上用场.我已经尝试过了.
This piece of code might come in handy. I have tried it.
byte[] imagedata = (byte [])dataGridView1[4, dataGridView1.SelectedRows[0].Index].Value;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(imagedata, 0, imagedata.Length))
{
ms.Write(imagedata, 0, imagedata.Length);
//Set image variable value using memory stream.
image = Image.FromStream(ms, true );
}