Stream转换为Windows

Stream转换为Windows

本文介绍了有没有办法到的System.IO.Stream转换为Windows.Storage.Streams.IRandomAccessStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows 8中;我想一个MemoryStream的内容传递给接受型Windows.Storage.Streams.IRandomAccessStream的参数的类。有什么办法来此的MemoryStream转换为IRandomAccessStream?

解决方案

要使用的扩展名:必须添加使用System.IO

在Windows8中,.NET和WinRT的类型通常是转换为/从引擎盖下兼容的类型,所以你不必在意它。

对于流,但是,也有辅助的方法来的WinRT和.NET流之间转换:对于来自WinRT的流转换 - > .NET流:

  InMemoryRandomAccessStream win8Stream =的GetData(); //从某处获取的数据流。
的System.IO.Stream的InputStream = win8Stream.AsStream()
 

有关转换从.NET流 - > WinRT的流:

  Windows.Storage.Streams.IInputStream inStream中= stream.AsInputStream();
Windows.Storage.Streams.IOutputStream outStream = stream.AsOutputStream();
 

更新:2013年9月1日

让这岂不是说,微软不听它的开发者社区;)

在<一个href="http://blogs.msdn.com/b/dotnet/archive/2013/06/26/announcing-the-net-framework-4-5-1-$p$pview.aspx">announcement对于.NET FX 4.5.1 ,微软表示:

通过网络I / O

  //获取图像
    VAR IMAGEURL =htt​​p://www.microsoft.com/global/en-us/news/publishingimages/logos/MSFT_logo_Web.jpg;
    VAR的客户=新的HttpClient();
    流流=等待client.GetStreamAsync(IMAGEURL);
    VAR memStream =新的MemoryStream();
    等待stream.CopyToAsync(memStream);
    memStream.Position = 0;
    VAR位图=新的BitmapImage();
    bitmap.SetSource(memStream.AsRandomAccessStream());
    image.Source =位图;
 

HTH。

In Windows 8; I would like to pass the contents of a MemoryStream to a class that accepts a parameter of type Windows.Storage.Streams.IRandomAccessStream. Is there any way to convert this MemoryStream to an IRandomAccessStream?

解决方案

To use the extensions: you must add "using System.IO"

In Windows8, .NET and WinRT types are generally converted to/from compatible types under the hood so you don't have to care about it.

For streams, however, there are helper methods to convert between WinRT and .NET streams:For converting from WinRT streams -> .NET streams:

InMemoryRandomAccessStream win8Stream = GetData(); // Get a data stream from somewhere.
System.IO.Stream inputStream = win8Stream.AsStream()

For converting from .NET streams -> WinRT streams:

Windows.Storage.Streams.IInputStream inStream = stream.AsInputStream();
Windows.Storage.Streams.IOutputStream outStream = stream.AsOutputStream();

UPDATE: 2013-09-01

Let it not be said that Microsoft doesn't listen to it's developer community ;)

In the announcement for .NET FX 4.5.1, Microsoft states that:

    //access image via networking i/o
    var imageUrl = "http://www.microsoft.com/global/en-us/news/publishingimages/logos/MSFT_logo_Web.jpg";
    var client = new HttpClient();
    Stream stream = await client.GetStreamAsync(imageUrl);
    var memStream = new MemoryStream();
    await stream.CopyToAsync(memStream);
    memStream.Position = 0;
    var bitmap = new BitmapImage();
    bitmap.SetSource(memStream.AsRandomAccessStream());
    image.Source = bitmap;

HTH.

这篇关于有没有办法到的System.IO.Stream转换为Windows.Storage.Streams.IRandomAccessStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:23