2013-07-20 33 views
0

我試圖讓BitmapImage系列化上的Windows Phone 8工作,但似乎很多圖書館都是從WP SDK缺失相比,桌面C#應用程序...字節數組的BitmapImage WP

基本上我有一個Byte陣列,我需要解析爲BitmapImage進行顯示,但是我在網上找不到任何東西......任何幫助都非常感謝! :)

由於StackOverflow上的算法,認爲這個問題太瑣碎,我只是要粘貼,我已經得到工作的BitmapImage的轉換爲ByteArray

public static Byte[] ImageToByteArray(BitmapImage image) 
    { 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      WriteableBitmap btmMap = new WriteableBitmap 
       (image.PixelWidth, image.PixelHeight); 

      Extensions.SaveJpeg(btmMap, ms, 
       image.PixelWidth, image.PixelHeight, 0, 100); 

      return ms.ToArray(); 
     } 
    } 

回答

3
public static BitmapImage ByteArraytoBitmap(Byte[] byteArray) 
{ 
    MemoryStream stream = new MemoryStream(byteArray); 
    BitmapImage bitmapImage = new BitmapImage(); 
    bitmapImage.SetSource(stream); 
    return bitmapImage; 
} 
+0

感謝代碼很多!我曾嘗試使用流預先,但我用'DataReader'而不是'MemoryStream' :) – user1123530

+0

你使用的是什麼版本的框架?我無法在BitmapImage中找到SetSource方法! – arturn