简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM