c#winfrom如何把SQL数据库中的二进制图片取出(多张),循环显示在pictureBox控件上? 20
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
{
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
追问
出错 无法将类型为“System.Int32”的对象强制转换为类型“System.Byte[]”
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询