metro中stream转IRandomAccessStream

下面的例子是从zip压缩文件解压出jpg文件流,再显示出来。

 

StorageFile zip = await KnownFolders.PicturesLibrary.GetFileAsync("1.zip");            

using (ZipArchive archive = new ZipArchive((await zip.OpenAsync(FileAccessMode.ReadWrite)).AsStream(), ZipArchiveMode.Update))       
{
        InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
        using (var stream = archive.GetEntry("1.jpg").Open())
       {
            await stream.CopyToAsync(ras.AsStreamForWrite()); //Stream转成IRandomAccessStream
            await ras.FlushAsync();
            ras.Seek(0); //这句是关键,必须把流的起点重新设置
            BitmapImage bi = new BitmapImage();
            bi.SetSource(ras);
            img.Source = bi; } }

 

posted @ 2014-03-06 11:33  wangjinming  阅读(295)  评论(0编辑  收藏  举报