在Windows Phone 8.1中将字节数组转换为图像的同步方法

时间:2014-05-12 15:41:29

标签: c# binding bytearray ivalueconverter windows-phone-8.1

我有一个带有序列化音乐信息的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

解决此问题的唯一方法是使用Dispatcher,它使代码异步,因此与IValueConverter不兼容...

我该怎么做才能使这项工作?

1 个答案:

答案 0 :(得分:-1)

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

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

相关问题