在 Windows Phone 上将 base64 字符串转换为 C# 中的图像 [英] Convert base64 string to image in C# on Windows Phone

查看:26
本文介绍了在 Windows Phone 上将 base64 字符串转换为 C# 中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 base64 字符串,我想将其转换为图像并将 Image 控件的 Source 设置为其结果.

I have a base64 string and I want convert that to an image and set the Source of an Image control to the result of that.

通常我会使用 Image.FromStream 来做到这一点,类似于:

Normally I would do that using Image.FromStream, similar to this:

Image img;
byte[] fileBytes = Convert.FromBase64String(imageString);
using(MemoryStream ms = new MemoryStream())
{
    ms.Write(fileBytes, 0, fileBytes.Length);
    img = Image.FromStream(ms);
}

但是,Image.FromStream 方法在 Windows Phone 上不存在,随便搜索只会找到依赖于该方法的结果.

However, the Image.FromStream method does not exist on Windows Phone, and a casual search only turns up results that depend on that method.

推荐答案

你可以使用这样的方法:

You can use a method like this:

    public static BitmapImage base64image(string base64string)
    {
        byte[] fileBytes = Convert.FromBase64String(base64string);

        using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
        {
            ms.Write(fileBytes, 0, fileBytes.Length);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(ms);
            return bitmapImage;
        }
    }

将图像添加到您的 XAML,例如:

Add an image to your XAML, such as this:

    <Image x:Name="myWonderfulImage" />

然后您可以设置源,如下所示:

You can then set the source, like this:

myWonderfulImage.Source = base64image(yourBase64string);

这篇关于在 Windows Phone 上将 base64 字符串转换为 C# 中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆