BASE64字符串转换成图像在C#在Windows Phone转换成、字符串、图像、Windows

2023-09-02 01:50:51 作者:小苹果ゝ冭孖

我有一个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);
 
精彩推荐
图片推荐