在 Windows 8 中;我想将 MemoryStream 的内容传递给接受 Windows.Storage.Streams.IRandomAccessStream 类型参数的类。有没有办法将此 MemoryStream 转换为 IRandomAccessStream?
7 回答
要使用扩展:您必须添加“使用 System.IO”
在 Windows8 中,.NET 和 WinRT 类型通常在后台转换为兼容类型/从兼容类型转换,因此您不必关心它。
但是,对于流,有一些帮助方法可以在 WinRT 和 .NET 流之间进行转换:对于从 WinRT 流 -> .NET 流进行转换:
InMemoryRandomAccessStream win8Stream = GetData(); // Get a data stream from somewhere.
System.IO.Stream inputStream = win8Stream.AsStream()
从 .NET 流转换 -> WinRT 流:
Windows.Storage.Streams.IInputStream inStream = stream.AsInputStream();
Windows.Storage.Streams.IOutputStream outStream = stream.AsOutputStream();
更新:2013-09-01
不要说微软不听它的开发者社区;)
在.NET FX 4.5.1 的公告中,微软声明:
你们中的许多人一直想要一种将 .NET 流转换为 Windows 运行时 IRandomAccessStream 的方法。让我们称之为 AsRandomAccessStream 扩展方法。我们无法将此功能添加到 Windows 8 中,但它是我们首次添加到 Windows 8.1 Preview 中的功能之一。
您现在可以编写以下代码,使用 HttpClient 下载图像,将其加载到 BitmapImage 中,然后设置为 Xaml Image 控件的源。
//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。
找到了一个更优雅的解决方案:
public static class MicrosoftStreamExtensions
{
public static IRandomAccessStream AsRandomAccessStream(this Stream stream)
{
return new RandomStream(stream);
}
}
class RandomStream : IRandomAccessStream
{
Stream internstream;
public RandomStream(Stream underlyingstream)
{
internstream = underlyingstream;
}
public IInputStream GetInputStreamAt(ulong position)
{
//THANKS Microsoft! This is GREATLY appreciated!
internstream.Position = (long)position;
return internstream.AsInputStream();
}
public IOutputStream GetOutputStreamAt(ulong position)
{
internstream.Position = (long)position;
return internstream.AsOutputStream();
}
public ulong Size
{
get
{
return (ulong)internstream.Length;
}
set
{
internstream.SetLength((long)value);
}
}
public bool CanRead
{
get { return this.internstream.CanRead; }
}
public bool CanWrite
{
get { return this.internstream.CanWrite; }
}
public IRandomAccessStream CloneStream()
{
throw new NotSupportedException();
}
public ulong Position
{
get { return (ulong)this.internstream.Position; }
}
public void Seek(ulong position)
{
this.internstream.Seek((long)position, SeekOrigin.Begin);
}
public void Dispose()
{
this.internstream.Dispose();
}
public Windows.Foundation.IAsyncOperationWithProgress ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
{
return this.GetInputStreamAt(this.Position).ReadAsync(buffer, count, options);
}
public Windows.Foundation.IAsyncOperation FlushAsync()
{
return this.GetOutputStreamAt(this.Position).FlushAsync();
}
public Windows.Foundation.IAsyncOperationWithProgress WriteAsync(IBuffer buffer)
{
return this.GetOutputStreamAt(this.Position).WriteAsync(buffer);
}
}
经过一些试验,我发现以下代码可以正常工作。
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage.Streams;
partial class MainPage
{
public MainPage()
{
var memoryStream = new MemoryStream(new byte[] { 65, 66, 67 });
ConvertToRandomAccessStream(memoryStream, UseRandomAccessStream);
InitializeComponent();
}
void UseRandomAccessStream(IRandomAccessStream stream)
{
var size = stream.Size;
} // put breakpoint here to check size
private static async void ConvertToRandomAccessStream(MemoryStream memoryStream,
Action<IRandomAccessStream> callback)
{
var randomAccessStream = new InMemoryRandomAccessStream();
var outputStream = randomAccessStream.GetOutputStreamAt(0);
var dw = new DataWriter(outputStream);
var task = new Task(() => dw.WriteBytes(memoryStream.ToArray()));
task.Start();
await task;
await dw.StoreAsync();
var success = await outputStream.FlushAsync();
callback(randomAccessStream);
}
}
更新:我还尝试了更优雅的方法实现:
private static void ConvertToRandomAccessStream(MemoryStream memoryStream,
Action<IRandomAccessStream> callback)
{
var randomAccessStream = new InMemoryRandomAccessStream();
var outputStream = randomAccessStream.GetOutputStreamAt(0);
RandomAccessStream.Copy(memoryStream.AsInputStream(), outputStream);
callback(randomAccessStream);
}
奇怪的是,它不起作用。当我stream.Size
稍后打电话时,我得到零。
更新我更改了函数以返回 IRandomAccessStream 而不是使用回调函数
public static async Task<IRandomAccessStream> ConvertToRandomAccessStream(MemoryStream memoryStream)
{
var randomAccessStream = new InMemoryRandomAccessStream();
var outputStream = randomAccessStream.GetOutputStreamAt(0);
var dw = new DataWriter(outputStream);
var task = new Task(() => dw.WriteBytes(memoryStream.ToArray()));
task.Start();
await task;
await dw.StoreAsync();
await outputStream.FlushAsync();
return randomAccessStream;
}
Windows 8 上没有内置方式方法。对于 Windows 8.1,我们添加了 Stream.AsRandomAccessStream() 扩展方法:
internal static IRandomAccessStream ToRandomAccessStream(byte[] array)
{
MemoryStream stream = new MemoryStream(array);
return stream.AsRandomAccessStream();
}
今天以上都不适用于我(可能在发布答案后发生了一些 API 更改)。唯一可行的方法是
IRandomAccessStream inMemoryStream = new InMemoryRandomAccessStream();
using (var inputStream = stream.AsInputStream())
{
await RandomAccessStream.CopyAsync(inputStream, inMemoryStream);
}
inMemoryStream.Seek(0);
此代码片段将流 ( stream
) 转换为 InMemoryRandomAccessStream( ims
) 实现IRandomAccessStream
。诀窍是必须在后台线程上调用 CopyTo。
InMemoryRandomAccessStream ims = new InMemoryRandomAccessStream();
var imsWriter = ims.OpenWrite();
await Task.Factory.StartNew(() => stream.CopyTo(imsWriter));
看看这个链接:
如何将字节数组转换为 IRandomAccessStream
它还提供了字节数组构造函数的示例和实现(以及一个用于 .NET 流的构造函数),如果您想使用类的SetSource
orSetSourceAsync
方法BitmapImage
(如我的例子),这很有用。
希望这可以帮助某人...