图片从MediaLibrary到Base64字符串

本文关键字:Base64 字符串 MediaLibrary | 更新日期: 2023-09-27 18:26:11

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

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字符串?

图片从MediaLibrary到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);
}