C#winform中如何在picturebox显示数据库中的二进制图片的具体代码
3个回答
展开全部
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
{
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询