Search 
DailyCoding > General

Convert Image to Base64 String and Base64 String to Image

Learn how to convert Image to Base64 String and Base64 String to Image
Author admin on Jun 6, 2008 4 Comments
Rate it    (Rated 3 by 3 people)
1,296 Views

This article will help you to learn how we can convert an image into a base64 string and base64 string back to image.

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

Base64 String to Image

public Image Base64ToImage(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;
}
C# | Utility

Discussion

bill On Jun 13, 2008 06:27 AM
i was looking for this
thanks

nour On Jul 14, 2008 08:15 AM
I was looking for this method for a while ,so thanks but i still have a problem i couldn't find the suitable java package for "Convert.ToBase64String" when i compile it's underlined as an error

Daily Coder On Jul 23, 2008 02:42 AM
I am not a java guy, but still let me know in case this article helps you out
http://www.wikihow.com/Encode-a-String-to-Base64-With-Java

anon On Aug 12, 2008 11:19 AM
this helped me. thanks

Leave a Comment

Name
Email Address
Web Site
© Copyright 2008 Daily Coding • All rights reserved