如何从数据库检索图像在c#

一个错误显示说,我有这个代码无效参数... 任何人都可以告诉我什么是错的?我应该得到分配给clientID的图像。如何从数据库检索图像在c#

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

}

回答:

这段代码可能派上用场。我试过了。

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

}

以上是 如何从数据库检索图像在c# 的全部内容, 来源链接: utcz.com/qa/260409.html

回到顶部