WinRt - FileSavePicker,将图像从URI保存到文件

时间:2015-03-16 10:00:19

标签: c# windows windows-runtime windows-8.1

我想问一下如何将图像从流保存到文件。我创建了这个FileSavePicker,但我不知道如何从Uri中保存图像

由于

  StreamReader reader = new StreamReader(dataStream);
                // Read the content.
               string responseFromServer = reader.ReadLine(); // URi with image


 private async void saveClick(object sender, RoutedEventArgs e)
    {
       var Picker = new FileSavePicker();
       Picker.FileTypeChoices.Add("Image", new List<string>() { ".jpg" });
       StorageFile file = await Picker.PickSaveFileAsync();



    }

1 个答案:

答案 0 :(得分:1)

我假设您已经将图像下载到dataStream中。如果没有,你可以使用HttpClient类:

var uri = new Uri("http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo-med.png");
Windows.Web.Http.HttpClient httpClient = new HttpClient();
var stream = await httpClient.GetInputStreamAsync(uri);
Stream dataStream = stream.AsStreamForRead();

您可以通过在StorageFile上调用OpenStreamForWriteAsync来获取可选择的文件流。使用两个流,您可以调用CopyTo从dataStream复制到保存流。

var Picker = new FileSavePicker();
Picker.FileTypeChoices.Add("Image", new List<string>() { ".jpg" });
StorageFile file = await Picker.PickSaveFileAsync();
using (Stream saveStream = await file.OpenStreamForWriteAsync())
{
    dataStream.Seek(0, SeekOrigin.Begin);
    await dataStream.CopyToAsync(saveStream);
}