简体   繁体   English

图片从MediaLibrary到Base64字符串

[英]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? 我的问题是现在如何将Picture转换为Base64字符串?

Get the Stream from the Picture instance using the GetStream method. 使用GetStream方法从Picture实例获取Stream Get the byte array from the stream. 从流中获取字节数组。 Convert bytes into the Base64 string using the Convert.ToBase64String method. 使用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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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