AndroidAL
AndroidAL

Reputation: 1119

Converting Base64 String to PictureBox

Im able to convert an image into Base64. But now im trying to convert it back and store it in a 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 =????;
}

Edit 1; Firstly Thanks to everyone, i have tried all the solutions below and they all work. But i have multiple images, What if there is no image in Poduct.Picture?

Upvotes: 7

Views: 15581

Answers (3)

M.S.
M.S.

Reputation: 4423

 // 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);
}

Upvotes: 4

sumngh
sumngh

Reputation: 566

just do it like this:

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;
}

Upvotes: 1

Roman Marusyk
Roman Marusyk

Reputation: 24609

Try to use something like:

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

Upvotes: 11

Related Questions