WinRt - FileSavePicker,将图像从 URI 保存到文件
WinRt - FileSavePicker, save image from URI to file
我想问一下如何将流中的图像保存到文件中。我已经创建了这个 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();
}
我假设您已经将图像下载到数据流中。如果没有,您可以使用 HttpClient class:
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 以从数据流复制到保存流。
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);
}
我想问一下如何将流中的图像保存到文件中。我已经创建了这个 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();
}
我假设您已经将图像下载到数据流中。如果没有,您可以使用 HttpClient class:
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 以从数据流复制到保存流。
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);
}