图片从MediaLibrary转Base64字符串

Picture from MediaLibrary to Base64 String

我有以下代码从相机胶卷中检索图片:

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

我现在的问题是如何将 Picture 转换为 Base64 字符串?

使用 GetStream 方法从 Picture 实例中获取 Stream。从流中获取字节数组。使用 Convert.ToBase64String 方法将字节转换为 Base64 字符串。

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