C# 从MediaLibrary到Base64字符串的图片

C# 从MediaLibrary到Base64字符串的图片,c#,image,windows-phone-8,base64,C#,Image,Windows Phone 8,Base64,我有以下代码从相机卷中检索图片: private string getBase64Image(Geophoto item) { MediaLibrary mediaLibrary = new MediaLibrary(); var pictures = mediaLibrary.Pictures; foreach (var picture in pictures) { var camerarollPath = picture.GetPath();

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

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

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

使用
GetStream
方法从
Picture
实例获取
流。从流中获取字节数组。使用
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);
}

我可以想象您想要将ImagePath(不确定它是什么数据类型)转换为字符串,然后var bytes=Encoding.UTF8.GetBytes(stringToConvert);var base64=Convert.tobase64字符串(字节);item.ImagePath仅包含图像的路径。我需要转换图像本身,而不是图像的路径…获取图像对象,将图像转换为字节。将字节转换为Base64字符串谢谢,我只需将picture.GetStream()更改为picture.GetImage(),谢谢您的评论。你是对的,我已经更新了答案。