C# Windows Phone 8.1中字节数组到图像的同步转换方法

C# Windows Phone 8.1中字节数组到图像的同步转换方法,c#,binding,bytearray,ivalueconverter,windows-phone-8.1,C#,Binding,Bytearray,Ivalueconverter,Windows Phone 8.1,我有一个带有序列化音乐信息的StorageFile,在启动应用程序时反序列化这些信息。我需要存储albumart图片,但因为BitmapImage无法序列化,所以我使用字节数组。我想将字节数组(它是ObservableCollection的一部分)绑定到图像源。为此,我需要使用IValueConverter将字节数组转换为BitmapImage 我的问题是IValueConverter是一种同步方法,我似乎找不到一种同步方法来转换BitmapImage中的字节数组 我试过这个: byte[] i

我有一个带有序列化音乐信息的StorageFile,在启动应用程序时反序列化这些信息。我需要存储albumart图片,但因为BitmapImage无法序列化,所以我使用字节数组。我想将字节数组(它是ObservableCollection的一部分)绑定到图像源。为此,我需要使用IValueConverter将字节数组转换为BitmapImage

我的问题是IValueConverter是一种同步方法,我似乎找不到一种同步方法来转换BitmapImage中的字节数组

我试过这个:

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;
}
这会引发一个异常:

The application called an interface that was marshalled for a different thread
解决这个问题的唯一方法是使用一个调度程序,它使代码异步,因此与IValueConverter不兼容


我应该怎么做才能使它工作?

我有一个类似的字节数组,我希望将它设置为图像源。 我能够使用BitmapImage显示图像


但是,绑定不起作用。您能指定如何绑定字节数组吗?

您能发布更多代码吗?在哪里实现上述方法?您希望同步运行什么?我通过将storagefile的路径绑定到imagesource:)解决了这个问题。最后,我将图像保存到内存中,并绑定了绝对文件路径。我尝试了这样做。但是它占用了太多的内存!要处理的图像太多。您知道将IValueConverter与异步和返回任务一起使用是否可行吗?@PhilippeMaes:终于找到了减少字节数组大小并直接绑定它的方法(WP8.1运行时)。压缩:绑定:您也可以更新代码。:)