c# – 如何从数据库中将图像显示到图片框中?

我使用以下代码.

foreach (DataRow dr1 in dt.Rows)
{
   picbyte = (byte[])dr1["AssociateImageData"];
   Stream stream = new MemoryStream(picbyte);
   pictureBox1.Image =Image.FromStream(stream,true,true);
}

最佳答案 试试这样:

var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds, "AssociateImageData");
int count = ds.Tables["AssociateImageData"].Rows.Count;

if (count > 0)
{ 
    var picbyte = (Byte[])(ds.Tables["AssociateImageData"].Rows[count - 1]["AssociateImageData"]);
    var stream = new MemoryStream(picbyte);
    pictureBox1.Image= Image.FromStream(stream);
} 
点赞