2010-07-26 105 views
5

出於遊戲的目的,我需要使用bitmapEncoder及其子類在一個二進制文件中通過WPF應用程序序列化一些圖片。byte [] to bitmapImage in silverlight

但是這些類在silverlight中不可用,所以我無法將它們從相同的二進制文件加載到瀏覽器中。

有人知道如何將一個字節[]轉換爲Silverlight中的BitmapImage嗎?

感謝,

風箏

回答

8

嘗試是這樣的:

BitmapImage GetImage(byte[] rawImageBytes) 
{ 
    BitmapImage imageSource = null; 

    try 
    { 
     using (MemoryStream stream = new MemoryStream(rawImageBytes )) 
     { 
      stream.Seek(0, SeekOrigin.Begin); 
      BitmapImage b = new BitmapImage(); 
      b.SetSource(stream); 
      imageSource = b; 
     } 
    } 
    catch (System.Exception ex) 
    { 
    } 

    return imageSource; 
} 
0

使用這種方法 第一次使用

using System.IO; 
using System.Windows.Media.Imaging; 

then

public Image Base64ToImage(byte[] imageBytes) 
     { 
      Image img = new Image(); 
      using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) 
      { 
       BitmapImage im = new BitmapImage(); 
       im.SetSource(ms); 
       img.Source = im; 
      } 
      return img; 
     }