2015-07-20 54 views
1

我試圖實現一個Windows通用應用程序。我面臨將圖像原始數據(字節數組)轉換爲BitmapImage控件的一個問題。我不知道圖像原始數據的類型。我用下面的代碼,將圖像原始數據轉換爲Windows中的bitmapimage通用應用程序

private async Task<BitmapImage> ByteArrayToBitmapImage(byte[] byteArray) 
{ 
    var bitmapImage = new BitmapImage(); 

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

    bitmapImage.SetSource(stream); 
    return bitmapImage; 
} 

圖像不顯示在窗口中。當我調試,發現的BitmapImage對象的身高&寬度爲0

如果有人知道這個解決方案,請幫我

回答

1

最後我的問題解決了,當我用下面的代碼,

var bmp = new WriteableBitmap(320, 240); 
    using (var stream = bmp.PixelBuffer.AsStream()) 
    { 
     stream.Write(bytes, 0, bytes.Length); 
     myImage.Source = bmp; 
    } 
0

我使用相同的代碼,並在我的作品,但我創建的BitmapImage並在調度員的線程上設置其源代碼 - 也許這是您的問題的關鍵。

+0

我試了一下。但沒有希望..似乎BitmapImage對象的pixelheight&pixelWidth屬性爲零。 –

+0

這裏的圖像原始數據不包含標題數據..這是問題。 –

相關問題