我正在开发Windows 8商店应用程序.我是新来的.
我正在以字节数组(byte [])的形式接收图像.
我必须将其转换回Image并在Image Control中显示它.
到目前为止我在屏幕上有按钮和图像控件.当我点击按钮时,我调用以下功能
private async Task LoadImageAsync()
{
byte[] code = //call to third party API for byte array
System.IO.MemoryStream ms = new MemoryStream(code);
var bitmapImg = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
Windows.Storage.Streams.InMemoryRandomAccessStream imras = new Windows.Storage.Streams.InMemoryRandomAccessStream();
Windows.Storage.Streams.DataWriter write = new Windows.Storage.Streams.DataWriter(imras.GetOutputStreamAt(0));
write.WriteBytes(code);
await write.StoreAsync();
bitmapImg.SetSourceAsync(imras);
pictureBox1.Source = bitmapImg;
}
Run Code Online (Sandbox Code Playgroud)
这不能正常工作.任何的想法?当我调试时,我可以看到以ms为单位的字节数组.但它没有转换为bitmapImg.
我在Codeproject上找到了以下内容
public class ByteImageConverter
{
public static ImageSource ByteToImage(byte[] imageData)
{
BitmapImage biImg = new BitmapImage();
MemoryStream ms = new MemoryStream(imageData);
biImg.BeginInit();
biImg.StreamSource = ms;
biImg.EndInit();
ImageSource imgSrc = biImg as ImageSource;
return imgSrc;
}
}
Run Code Online (Sandbox Code Playgroud)
此代码应该适合您.