2

This question is specific to Windows Store App (Windows 8).

I'm trying to convert a Bitmapimage to Byte[] and store it in a xml file, then retrieve and convert it back to Bitmapimage.

I was able to convert the Bitmapimage to byte[] (although it's not the best way, so I'm not including example). Then I'm using serialization to convert a class to xml. Here's the attribute used to serialize byte[]:

    private byte[] iconBytes;
    [XmlAttribute(DataType = "base64Binary")]
    public byte[] IconBytes
    {
        get 
        { 
            // TO DO: Convert BitmapImage to Byte[]
            return this.iconBytes; 
        }
        set 
        { 
            this.SetProperty(ref this.iconBytes, value);
            // TO DO: Convert Byte[] to BitmapImage
        }
    }

And here is the XML generated:

<MyApp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Groups>
<Group Title="Group1">
  <Items>
    <Item Title="Item1" IconBytes="7uLC/+7iwv/u4sL/7uLC/0RVZf9heJL/X3OH/+vbvP/r27b/69u2/+vbtv/r27b/7d+9/+3fvf/r27z/R2aO/2Z2t/92h9T/obH3/3uXxP/lz57/5c+e/+XPnv/lz57/6dW3/+nVt//azbf/Um6o/2Jqu/9ib77/lqbp/4uq5//gw5T/4MOU/+DDlP/iyJj/5tCy/97Akf/Mt5b/Ulh+/ygpZv82P47/HhpG/01rpv+Npqn/3LuM/9y7jP/ewJH/27KF/9uyhf+xrKf/VWet/1hatf9reND/UVzD/5Sp8f+qmoL/27KF/9uyhf/ft5H/1q2A/9atgP/WrYD/QU6Y/zY2eP8wKFf/cHit/1ZtsP/WrYD/1q2A/9atgP/bsoX/0KF1/9Chdf/QoXX/NkqC/z5Bh/9KRaH/X3W4/zRMf//QoXX/0KF1/9Chdf/UqXz/zZlv/82Zb/8yJCD/Nj5u/0FOkP9FS5j/Wm+2/09rov9CMTP/zZlv/82Zb//QoXX/KBkV/yUWE/8eEg3/rKCc/ycmUP8yOnL/PU6E/2Nfdf9CLSj/LR0Y/zwnIf9fUUT/IxMQ/yASDP8dDwz/dWpp/4eGiP82QHT/QT5h/4l2c/80IB3/NCAc/yoZFf81IR//IBQO/x0QDP8dDgv/HQ4L/5eTkf+OmKL/k5KT/zAeG/8tHhf/KRsU/zIfGv8tHhf/IREN/x8PC/8fEAz/HhEM/7Suq/+dnaL/qqek/zAeGv8hFA//KBkU/ygaFP8uGhb/" />
  </Items>
</Group>
</Groups>
</MyApp>

Once I deserialize the class from the xml, how can I convert byte[] to BitmapImage?

There are several examples out there addressing this issue, but they are all related to Silverlight and WPF, and none of them translate successfully to Windows Store App (Windows 8).

Please keep in mind that examples using streams from the web or local computer does not apply to this issue. The image byte[] is already in memory because the data was deserialized from an xml file.

Any help is greatly appreciated. Happy New Year!

Richard H
  • 300
  • 7
  • 15

1 Answers1

1

You could try using InMemoryRandomAccessStream:

var stream = new InMemoryRandomAccessStream();
await stream.WriteAsync(iconBytes.AsBuffer());
stream.Seek(0);

var image = new BitmapImage();
await image.SetSourceAsync(stream);

Here's another example.

Damir Arh
  • 17,637
  • 2
  • 45
  • 83
  • Thanks @Damir for pointing me in the right direction, I found [another posting](http://stackoverflow.com/questions/13529301/load-bitmapimage-from-base64string?answertab=active#tab-top) that seems to work for the person that asked, still not working for me. I believe the Base64 string that I'm using is incorrect. Any suggestions on how to convert a BitmapImage into Base64? – Richard H Jan 06 '13 at 06:21
  • @YYZRichard I corrected my answer by adding a missing seek to the beginning of the memory stream. I have tested the code now and it works. I haven't managed to load the image from your XML, though. Indeed there seems to be something wrong with it, since no image viewer opens it up if I save it to a file. Take a look at [this question](http://stackoverflow.com/q/13423796/197913) for a working `BitmapImage` to `Base64` conversion. – Damir Arh Jan 06 '13 at 09:11
  • 2
    Thanks again @Damir, I added `Stream.Seek(0)`, and I'm now getting the error `The component cannot be found. (Exception from HRESULT: 0x88982F50)`, in the last line `await image.SetSourceAsync(stream);` – Richard H Jan 15 '13 at 03:43
  • @YYZRichard Yes, this happens because the byte array is not a valid image as you already suggested yourself. If you try loading the byte array from a valid file, the same code will work. Did you check [here](http://stackoverflow.com/a/7262418/197913) and [here](http://stackoverflow.com/a/13424285/197913) for a working conversion between `BitmapImage` and `Base64`? – Damir Arh Jan 15 '13 at 05:55