您好我想问一下如何将超过1张图片保存到文件夹中。我有图像的URI列表,我想将所有图像保存到选定的文件夹。这是我的保存方法,但它只保存列表中的最后一个图像。我需要保存名单中的所有图像,如img0,img1,img2,img3。
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();
foreach (var a in im) // im is list with images uri
{
var uri = new Uri(a.ImgUri.ToString());
Windows.Web.Http.HttpClient httpClient = new HttpClient();
var stream = await httpClient.GetInputStreamAsync(uri);
Stream dataStream = stream.AsStreamForRead();
using (Stream saveStream = await file.OpenStreamForWriteAsync())
{
await dataStream.CopyToAsync(saveStream);
await dataStream.FlushAsync();
}
}
}
答案 0 :(得分:0)
您只能写一个文件。每次将图像写入该文件时,文件都会被覆盖,因此只会保存最后一张图像。
您只需将以下代码块移动到foreach
循环中即可修复错误。
foreach (var a in im) // im is list with images uri
{
//Move the code into foreach
var Picker = new FileSavePicker();
Picker.FileTypeChoices.Add("Image", new List<string>() { ".jpg" });
StorageFile file = await Picker.PickSaveFileAsync();
var uri = new Uri(a.ImgUri.ToString());
...
}