Convert base64 string to image in C# on Windows Phone

后端 未结 1 1276
小鲜肉
小鲜肉 2020-12-06 19:36

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.

Normally I would do that using Image.F

相关标签:
1条回答
  • 2020-12-06 20:06

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

    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);
    
    0 讨论(0)
提交回复
热议问题