简体   繁体   中英

Synchronous way to convert byte array to image in Windows Phone 8.1

I have a StorageFile with serialized music information which I deserialize when starting the app. I need to store albumart picture, but because BitmapImage can't be serialized I use a byte array. I want to bind the byte array (which is part of a ObservableCollection) to the Source of an image. For this to work I need to convert the byte array to a BitmapImage using an IValueConverter.

My problem is that an IValueConverter is a synchronous method and I can't seem to find a synchronous way to convert a byte array in a BitmapImage...

I tried this:

byte[] imagedata = tag.Pictures[0].PictureData;
Debug.WriteLine("Byte array length: " + imagedata.Length.ToString());

using (MemoryStream ms = new MemoryStream(imagedata))
{
    // create IRandomAccessStream
    var albumartstream = ms.AsRandomAccessStream();
    albumartstream.Seek(0);

    // create bitmap and assign
    BitmapImage albumart = new BitmapImage();
    albumart.SetSourceAsync(albumartstream);

    // return
    return albumart;
}

This throws an exception:

The application called an interface that was marshalled for a different thread

The only way to solve this is to use a Dispatcher which makes the code asynchronous and therefor incompatible with the IValueConverter...

What should I do to make this work?

I have a similar byte array and I wish to set it as image source. I am being able to display the image using BitmapImage.

But, binding is not working. Could you please specify how to bind the byte array?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM