加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

在Windows Phone上将base64字符串转换为C#中的图像

发布时间:2020-12-14 02:56:07 所属栏目:Windows 来源:网络整理
导读:我有一个base64字符串,我想将其转换为图像并将 Image控件的Source设置为该结果. 通常我会使用Image.FromStream来做到这一点,类似于: Image img;byte[] fileBytes = Convert.FromBase64String(imageString);using(MemoryStream ms = new MemoryStream()){ ms
我有一个base64字符串,我想将其转换为图像并将 Image控件的Source设置为该结果.

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

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

但是,Windows Phone上不存在Image.FromStream方法,偶然搜索只会显示依赖于该方法的结果.

解决方法

你可以使用这样的方法:

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

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

将图像添加到XAML,例如:

<Image x:Name="myWonderfulImage" />

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

myWonderfulImage.Source = base64image(yourBase64string);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读