WinRT Convert Stream to BitmapImage
Win8项目中遇到问题:如何将一段stream转化成InMemoryRandomAccessStream, 后可继续转化为bitmapimage, 然后在UI中显示出来,google以及各大论坛搜索了很久,也有很多人问,但是一直没找到可用的示例。终于自己提取了各种高手的答案中的精华组成一段代码,经测试可用:
BitmapImage bitmapImage = newBitmapImage();
InMemoryRandomAccessStream ras = newInMemoryRandomAccessStream();
DataWriter dataWriter = newDataWriter(ras.GetOutputStreamAt(0));
byte[] buffer = newbyte[1024];
for (int size = streamToConvert.Read(buffer, 0, buffer.Length); size > 0; size = streamToConvert.Read(buffer, 0, buffer.Length))
{
byte[] tempBuffer = newbyte[size];
Array.Copy(buffer, tempBuffer, size);
dataWriter.WriteBytes(tempBuffer);
}
await dataWriter.StoreAsync();
bitmapImage.SetSource(ras);
希望大家用得到,也希望大家提出更好的方法。