簡體   English   中英

在Windows Phone 8.1中將字節數組轉換為圖像的同步方法

[英]Synchronous way to convert byte array to image in 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,該Dispatcher使代碼異步,因此與IValueConverter不兼容。

我應該怎么做才能使這項工作?

我有一個類似的字節數組,我希望將其設置為圖像源。 我能夠使用BitmapImage顯示圖像。

但是,綁定無效。 您能否指定如何綁定字節數組?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM