簡體   English   中英

圖片從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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM