BASE64字符串转换成图像在C#在Windows Phone [英] Convert base64 string to image in C# on Windows Phone

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

问题描述

我有一个base64字符串,我想将图像转换控制到这个结果的,要一个形象,并设置源。

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);

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

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