c#winfrom如何把SQL数据库中的二进制图片取出(多张),循环显示在pictureBox控件上?

这个问题困扰我好多天,希望能有个大神帮我解惑。
2024-11-18 11:33:43
推荐回答(1个)
回答1:

private void button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
try
{
using (SqlConnection conn = new SqlConnection(_ConnString))
{
SqlDataAdapter da = new SqlDataAdapter("SELECT ImageSource FROM tblUserImages WHERE UserID='12'", conn);
da.Fill(dt);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
byte[] imagedata = (byte[])(dt.Rows[0][0]);
MemoryStream myStream = new MemoryStream();
foreach (byte a in imagedata)
{
myStream.WriteByte(a);
}
Image myImage = Image.FromStream(myStream);
myStream.Close();
this.pictureBox1.Image = myImage;
this.pictureBox1.Refresh();
}
我是这样做的,从数据库里面查询出来先转换为字节,再转换为图片,亲测可行,还有什么不懂的可以问我,sunyijie110@qq.com