6

I'm trying to convert a Base64 string to an image and set ImageView using the same image. I know how to get it done in java, but I'm having a trouble in C#. Anyone have an idea of how to get it done in C#?

Some of the code I've tried;

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;
    }
 }
Joewy Lombe
  • 167
  • 1
  • 5
  • 13

3 Answers3

11

Base64 to Bitmap :

public Bitmap Base64ToBitmap(String base64String)
{
    byte[] imageAsBytes = Base64.Decode(base64String, Base64Flags.Default);
    return BitmapFactory.DecodeByteArray(imageAsBytes, 0, imageAsBytes.Length);
}

Bitmap to Base64 :

public String BitmapToBase64(Bitmap bitmap)
{
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.Compress(Bitmap.CompressFormat.Png, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.ToByteArray();
    return Base64.EncodeToString(byteArray, Base64Flags.Default);
}
Yksh
  • 3,276
  • 10
  • 56
  • 101
4

Image to Base64 String, online conversion - here

 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, online conversion - here

    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;
}
Vishwesh Jainkuniya
  • 2,821
  • 3
  • 18
  • 35
  • Hi, Vishwesh I've tried this code, i get the following error ; 'Image' does not contain a definition for 'FromStream' . Im using Visual Studio 2015 By the way. – Joewy Lombe May 02 '16 at 02:08
  • `MemoryStream ms = new MemoryStream(imageBytes);` is enough. You don't need to `Write` to it. In this form, you must also set the position to 0 before passing to FromStream – Vishwesh Jainkuniya May 02 '16 at 02:41
3

I'm assuming that your encoded data is some bitmap format? If so, you should be able to do something like this:

public Bitmap 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))
    {
        Bitmap image = BitmapFactory.FromStream(ms, true);
        return image;
    }
 }
Jason
  • 86,222
  • 15
  • 131
  • 146