在 c# 和 winrt 中将流保存到文件

时间:2023-03-09
本文介绍了在 c# 和 winrt 中将流保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 c# 和 winrt 中有:

I have in c# and winrt:

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

stream 是一个 Windows.Storage.Streams.IRandomAccessStream

所以我对 c# 和 winrt 完全陌生.我如何将此包含 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 对象创建的 DataReader 类,然后从流中读取大量 .NET 对象以及字节数组.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 中将流保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:在 C# 代理中从 Web 服务器读取图像 下一篇:PDFsharp 保存到 MemoryStream

相关文章

最新文章