ManfredP
ManfredP

Reputation: 1047

Picture from MediaLibrary to Base64 String

I have the following code to retrieve a picture from camera roll:

private string getBase64Image(Geophoto item)
{
    MediaLibrary mediaLibrary = new MediaLibrary();
    var pictures = mediaLibrary.Pictures;
    foreach (var picture in pictures)
    {
        var camerarollPath = picture.GetPath();
        if (camerarollPath == item.ImagePath)
        {
            // Todo Base64 convert here
        }
    }

    return "base64";
}

My question is now how to convert a Picture to a Base64 string?

Upvotes: 1

Views: 80

Answers (1)

Sergii Zhevzhyk
Sergii Zhevzhyk

Reputation: 4202

Get the Stream from the Picture instance using the GetStream method. Get the byte array from the stream. Convert bytes into the Base64 string using the Convert.ToBase64String method.

Stream imageStream = picture.GetImage();
using (var memoryStream = new MemoryStream())
{
    imageStream.CopyTo(memoryStream);
    byte[] buffer = memoryStream.ToArray();
    // this is the Base64 string you are looking for
    string base64String = Convert.ToBase64String(buffer);
}

Upvotes: 2

Related Questions