将Base64字符串转换为PictureBox

时间:2016-04-19 09:12:47

标签: c# sql-server image winforms base64

我能够将image转换为Base64。但现在我试图将其转换回来并将其存储在PictureBox

var pic = Convert.FromBase64String(product.Picture);

using (System.Drawing.Image image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(pic)))
{ 
      //NOT SURE WHAT TO DO HERE
      pictureBox1.Image =????;
}

编辑1; 首先感谢大家,我已经尝试了下面的所有解决方案,他们都工作。但我有多个图像,如果Poduct.Picture中没有图像怎么办?

3 个答案:

答案 0 :(得分:8)

尝试使用以下内容:

using (MemoryStream ms = new MemoryStream(pic))
{
     pictureBox1.Image = Image.FromStream(ms);
}

答案 1 :(得分:2)

 // Convert base 64 string to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
// Convert byte[] to Image
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
    pictureBox1.Image = Image.FromStream(ms, true);
}

答案 2 :(得分:1)

就这样做:

var pic = Convert.FromBase64String(product.Picture);

using (System.Drawing.Image image = System.Drawing.Image.FromStream(new  System.IO.MemoryStream(pic)))
{ 
  //NOT SURE WHAT TO DO HERE
  pictureBox1.Image =image;
}