图片已经以二进制流存储在数据库了,怎样把它从数据库获取显示在界面上啊?

2024-12-27 13:26:04
推荐回答(1个)
回答1:

//获取数据库里的图片代码(省略)
object ob = dataTable.Rows[0]["图片"];
byte[] buff = ob as byte[];
if (ob == null)
{
//报错
return;
}
MemoryStream ms = new MemoryStream(buff, 0, buff.Length, true);
Bitmap bt = Bitmap.FromStream(ms) as Bitmap;
if (bt == null)
{
//数据格式错误,请检查是否是图片
return;
}
ms.Flush();
ms.Close();
//把bt显示出来,比如 pictureBox.Image = bt;