0

I'm trying to convert base64 string to image and bind the result.

This is my xaml :

<Image Source="{Binding image64}">

To be sure my base64 string is correct i did that :

    public BitmapImage image64
    {
        get
        {
            **//Convert my path img to Base64.**
            byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes(image);
            string base64String = System.Convert.ToBase64String(bytes);
            MessageBox.Show("Base 64 String :[" + base64String + "]");



            //Convert my img base64 to img.
            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;
            }
        }
    }

This code don't work in my case because of setSource. I found this "solution" here : similar question 1 similar question 2

But they don't work in my case, i think it's because they didn't use the binding. And i don't have any idea to fix it...

Sorry for my english, and i hope someone can help me :)

Community
  • 1
  • 1
Fly_federer
  • 206
  • 1
  • 2
  • 12
  • Isn't your base64 sring in the `image` variable? In this case, you need to call directly `byte[] fileBytes = Convert.FromBase64String(image);` Also, don't forget to reset the position of your stream before reading from it: `ms.Position = 0` (right before the `bitmapImage.SetSource`) – Kevin Gosse Apr 17 '15 at 11:06
  • @KooKiz , I added the ms.position = 0, and i had a new mistake.**The component cannot be found. (Exception from HRESULT: 0x88982F50) ** Do you have any idea ? thanks for your answer :) – Fly_federer Apr 18 '15 at 12:13

1 Answers1

0

I found this solution, it works perfectly.

byte[] filebytes = Convert.FromBase64String(image);
                MemoryStream ms = new MemoryStream(filebytes, 0, filebytes.Length);
                BitmapImage mimage = new BitmapImage();
                mimage.SetSource(ms);
                return mimage;
Fly_federer
  • 206
  • 1
  • 2
  • 12