2012-09-22 108 views
0

我想將字節數組轉換爲XAML中的圖像。我知道我有一個從我的服務中返回的字節數組。我不確定爲什麼我的代碼失敗:Metro圖像字節陣列

public async void GetImage() 
    { 
     var serviceClient = new MyServiceClient(); 
     var prod = await serviceClient.GetProductImageAsync(pr.ProductID); 
     if (prod != null) 
     { 
      var ras = new InMemoryRandomAccessStream(); 
      BitmapImage bi = new BitmapImage(); 

      using (MemoryStream ms = new MemoryStream(prod.LargeImage, 0, 
       prod.LargeImage.Length)) 
      { 
       BitmapImage im = new BitmapImage(); 
       await ms.CopyToAsync(ras.AsStreamForWrite()); 
       im.SetSource(ras); 
       this.image1.Source = im; 
      } 
     } 
     else 
     { 
      MessageDialog messageDialog = new MessageDialog("There is no picture available for " + pr.Name, "No Image"); 
      messageDialog.ShowAsync(); 
     } 
     myBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;    
    } 
+0

失敗在什麼情況下? –

回答

0

不知道爲什麼它的失敗,是沒有任何理由不設置源作爲byte[]

im.SetSource(new MemoryStream(prod.GetBytes())); 
0

這裏是我使用的最終代碼:

namespace MenuFinderWin8.Pages 
{ 
/// <summary> 
/// A basic page that provides characteristics common to most applications. 
/// </summary> 
public sealed partial class ImagePage : MenuFinderWin8.Common.LayoutAwarePage 
{ 
    Product pr; 

    public ImagePage() 
    { 
     this.InitializeComponent(); 
     pr = App.CurrentProduct; 
     GetImage(); 
    } 


    public async void GetImage() 
    { 
     MenuFinderAppServiceClient serviceClient = new MenuFinderAppServiceClient(); 
     var prod = await serviceClient.GetProductImageAsync(pr.ProductID); 
     if (prod != null) 
     { 
      var randomAccessStream = new MemoryRandomAccessStream(prod.LargeImage); 
      BitmapImage bi = new BitmapImage(); 
      await bi.SetSourceAsync(randomAccessStream); 
      image1.Source = bi; 
     } 
     else 
     { 
      MessageDialog messageDialog = new MessageDialog("There is no picture available for " + pr.Name, "No Image"); 
      messageDialog.ShowAsync(); 
     } 
     myBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;    
    } 

    /// <summary> 
    /// Populates the page with content passed during navigation. Any saved state is also 
    /// provided when recreating a page from a prior session. 
    /// </summary> 
    /// <param name="navigationParameter">The parameter value passed to 
    /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested. 
    /// </param> 
    /// <param name="pageState">A dictionary of state preserved by this page during an earlier 
    /// session. This will be null the first time a page is visited.</param> 
    protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) 
    { 
    } 

    /// <summary> 
    /// Preserves state associated with this page in case the application is suspended or the 
    /// page is discarded from the navigation cache. Values must conform to the serialization 
    /// requirements of <see cref="SuspensionManager.SessionState"/>. 
    /// </summary> 
    /// <param name="pageState">An empty dictionary to be populated with serializable state.</param> 
    protected override void SaveState(Dictionary<String, Object> pageState) 
    { 
    } 
} 

class MemoryRandomAccessStream : IRandomAccessStream 
{ 
    private Stream m_InternalStream; 

    public MemoryRandomAccessStream(Stream stream) 
    { 
     this.m_InternalStream = stream; 
    } 



    public MemoryRandomAccessStream(byte[] bytes) 
    { 
     this.m_InternalStream = new MemoryStream(bytes); 
    } 

    public IInputStream GetInputStreamAt(ulong position) 
    { 
     this.m_InternalStream.Seek((long)position, SeekOrigin.Begin); 


     return this.m_InternalStream.AsInputStream(); 
    } 

    public IOutputStream GetOutputStreamAt(ulong position) 
    { 
     this.m_InternalStream.Seek((long)position, SeekOrigin.Begin); 


     return this.m_InternalStream.AsOutputStream(); 
    } 

    public ulong Size 
    { 
     get { return (ulong)this.m_InternalStream.Length; } 
     set { this.m_InternalStream.SetLength((long)value); } 
    } 

    public bool CanRead 
    { 
     get { return true; } 
    } 

    public bool CanWrite 
    { 
     get { return true; } 
    } 

    public IRandomAccessStream CloneStream() 
    { 
     throw new NotSupportedException(); 
    } 

    public ulong Position 
    { 
     get { return (ulong)this.m_InternalStream.Position; } 
    } 

    public void Seek(ulong position) 
    { 
     this.m_InternalStream.Seek((long)position, 0); 
    } 

    public void Dispose() 
    { 
     this.m_InternalStream.Dispose(); 
    } 

    public Windows.Foundation.IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) 
    { 
     var inputStream = this.GetInputStreamAt(0); 
     return inputStream.ReadAsync(buffer, count, options); 
    } 

    public Windows.Foundation.IAsyncOperation<bool> 


FlushAsync() 
    { 
     var outputStream = this.GetOutputStreamAt(0); 
     return outputStream.FlushAsync(); 
    } 

    public Windows.Foundation.IAsyncOperationWithProgress<uint, uint> 


WriteAsync(IBuffer buffer) 
    { 
     var outputStream = this.GetOutputStreamAt(0); 
     return outputStream.WriteAsync(buffer); 
    } 
} 

}