C# Base64 String to JPEG Image

The Solution to C# Base64 String to JPEG Image is

First, convert the base 64 string to an Image, then use the Image.Save method.

To convert from base 64 string to Image:

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

To convert from Image to base 64 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 base 64 string
    string base64String = Convert.ToBase64String(imageBytes);
    return base64String;

Finally, you can easily to call Image.Save(filePath); to save the image.

~ Answered on 2013-09-16 11:49:13

Most Viewed Questions: