Xamarin Android - Convert Image to Base 64

时间:2016-10-19 13:41:12

标签: c# android xamarin

I've found this sample code to convert an image to base 64, I'm not sure how to pass an image into it though. I want to be able to give a path to a specific directory. I've managed to find a file with this code:

byte[] ImageData = File.ReadAllBytes("storage/emulated/0/DCIM/Camera/img.jpg");

But I need to pass that into the following code.

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

Hope you can help. Thanks.

1 个答案:

答案 0 :(得分:1)

byte[] ImageData = File.ReadAllBytes(path_to_file);

string base64String = Convert.ToBase64String(ImageData);