Convert Base64 String to Bitmap or Image Xamarin or Android

14,701

Solution 1

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

Solution 2

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

Solution 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;
    }
 }
Share:
14,701

Related videos on Youtube

Joewy Lombe
Author by

Joewy Lombe

Updated on June 04, 2022

Comments

  • Joewy Lombe
    Joewy Lombe almost 2 years

    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
    Joewy Lombe almost 8 years
    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.
  • Vishwesh Jainkuniya
    Vishwesh Jainkuniya almost 8 years
    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