【问题标题】:Base64ToImage in Windows 8 Store AppWindows 8 应用商店应用中的 Base64ToImage
【发布时间】:2013-12-16 18:59:03
【问题描述】:

在我的 Windows 8 应用程序中,我得到一个图像作为 base64 字符串。现在我正在寻找一种转换字符串的方法,以便可以将其包含在我的 XAML 图像中,如下所示:

public static Image Base64ToImage(string s)
{
    // How To?
}

我见过很多解决方案,但它们都使用 Windows 8 商店应用程序中不可用的类/方法。感谢您的提示。

【问题讨论】:

    标签: image xaml windows-8 windows-store-apps base64


    【解决方案1】:

    该方法可能如下所示:

    private async Task<BitmapImage> Base64ToImage(string base64)
    {
        var bitmap = new BitmapImage();
        var buffer = Convert.FromBase64String(base64);
    
        using (var stream = new InMemoryRandomAccessStream())
        {
            await stream.WriteAsync(buffer.AsBuffer());
            stream.Seek(0);
            bitmap.SetSource(stream);
        }
    
        return bitmap;
    }
    

    【讨论】:

    • 优秀而简短的解决方案!我添加了一个 try/catch 块以防 base64 字符串无效。
    猜你喜欢
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多