Reading gif file from base64 into picturebox any other way? C#

Tags: c# picturebox
By : New Bie

I Have gif image in base64.

Currently I am approaching this way. reading base64 gif file and writing it to byte array and writing it back to image file to disk and reading from the file to picturebox.image.

 byte[] imageBytes = Convert.FromBase64String(body);
                //*  this is write file to disk and read
                string filename = Username;
                File.WriteAllBytes(filename, imageBytes);
                fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
                pictureBox1.Image = Image.FromStream(fs);

Now, I want to write it to memory without writing it to disk file. like in a form of variable image. that can be assigned to picturebox. Is there any possible of this. because I have to do repeatedly many times for many images.

So I would like find a different approach without writing saving file to disk and reading it again.

Any help appreciated.

By : New Bie

byte[] imageBytes = Convert.FromBase64String(body);
using (var ms = new MemoryStream(imageBytes))
    pictureBox1.Image = Image.FromStream(ms);

Be aware the MemoryStream class is IDisposable so you should Dispose() it. This can happen with using or with try/catch/finally block.

byte[] imageBytes = Convert.FromBase64String(body);
MemoryStream stream = new MemoryStream(imageBytes);
pictureBox1.Image = Image.FromStream(stream);

This video can help you solving your question :)
By: admin