我正在尝试将字节数组转换为XAML中的图像。我知道我的服务中有一个字节数组。我不确定为什么我的代码失败了:
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()));
答案 1 :(得分: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);
}
}
}