Convert Base64 String to Image

//Convert Base64 String to byte[]

byte[] imageBytes = Convert.FromBase64String(stringHere);

MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

//Convert byte[] to Image

ms.Write(imageBytes, 0 , imageBytes.Length);

System.Drawing.Image img = System.Drawing.Image.FromStream(ms, true);

img.Save(HttpRuntime.AppDomainAppPath + @”\test.png”);

Image1.Image = @”\test.png”;

Convert Base64 string to Image

public Image Base64StringToImage(string base64String){

//Convert Base64 String to byte[]

byte[] imageBytes = Convert.FromBase64String(base64String);

MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

//Convert byte[] to Image

ms.Write(imagebytes, 0, imageBytes.Length);

Image image = Image.FromStream(ms, true);

return image;

}

Convert Image to Base64 string

public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format){

using(MemoryStream ms = new MemoryStream()){

//Convert Image to byte[]

image.Save(ms, format);

byte[] imageBytes = ms.ToArray();

//Convert byte[] to Base64 string

string base64String = Convert.ToBase64String(imageBytes);

return base64String;

}

}

Load image

pictureBox1.Image = Image.FromFile(@”C:\Users\…jpg”);

OR

OpenFileDialog open = new OpenFileDialog();

open.Filter = “Image Files(*.jpg;*.jpeg;*.gif;*.bmp)|*.jpg;*.jpeg;*.gif;*.bmp”;

if(open.ShowDialog() == DialogResult.OK)

{

pictureBox1.Image = new Bitmap(open.FileName);

}

 

*In Window form, pictureBox does not take image from Image.FromStream, only from bitmap.