存储流在C#和WinRT的到文件 [英] save stream to file in c# and winrt

查看:155
本文介绍了存储流在C#和WinRT的到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#和WinRT的:

I have in c# and winrt:

var stream = await speech.GetSpeakStreamAsync(SpeechText.Text, language);



Windows.Storage.Streams.IRandomAccessStream

所以,我完全地新的C#和WinRT的。我该如何拯救这个流containg一个wav文件到文件?提前
谢谢,圣殿

So I am completly new to c# and winrt. How i save this stream containg a wav-file to a file? Thanks in advance, Basilius

推荐答案

该IRandomAccessStream有一个名为GetInputStreamAt的 http://msdn.microsoft.com/en-US/library/windows/apps/ windows.storage.streams.irandomaccessstream

The IRandomAccessStream has a method called GetInputStreamAt http://msdn.microsoft.com/en-US/library/windows/apps/windows.storage.streams.irandomaccessstream

IInputStream inputStream = stream.GetInputStreamAt(0);

这让你的IInputStream。

This gets you an IInputStream.

IInputStream接口定义只有一个方法,ReadAsync,它可以让你将字节读入一个IBuffer对象。 Windows.Storage.Stream也包括您创建一个基于IInputStream对象,然后从流读取大量的.NET对象以及字节数组一个DataReader类。 http://www.charlespetzold.com/blog/2011/11/080203.html http://msdn.microsoft.com/library/windows/apps/ BR208119

The IInputStream interface defines just one method, ReadAsync, which lets you read bytes into an IBuffer object. Windows.Storage.Stream also includes a DataReader class that you create based on an IInputStream object and then read numerous .NET objects from the stream as well as arrays of bytes. http://www.charlespetzold.com/blog/2011/11/080203.html , http://msdn.microsoft.com/library/windows/apps/BR208119

using (var stream = new InMemoryRandomAccessStream())
{
    // for example, render pdf page
    var pdfPage = document.GetPage((uint)i);
    await pdfPage.RenderToStreamAsync(stream);

    // then, write page to file
    using (var reader = new DataReader(stream))
    {
        await reader.LoadAsync((uint)stream.Size);
        var buffer = new byte[(int)stream.Size];
        reader.ReadBytes(buffer);
        await Windows.Storage.FileIO.WriteBytesAsync(file, buffer);
    }
}

现在你有一个包含所有读取的字节的缓冲区。

now you have a buffer containing all the read bytes.

您可以在这个缓冲区现在保存到文件中的 http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html

you can now save this buffer to a file http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html

var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("MyWav.wav", Windows.Storage.CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteBytesAsync(file, buffer);

这篇关于存储流在C#和WinRT的到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆