此代码从图片库中获取名称为face1.jpg,face2.jpg等的照片并显示它们。现在的问题是它适用于前9张图片然后停止。但它应该通过画廊中的所有图片
StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
IReadOnlyList<IStorageFile> file = await picturesFolder.GetFilesAsync(CommonFileQuery.OrderByDate);
string fname;
int picSize = 150;
int i = 0;
WriteableBitmap wv = new WriteableBitmap(picSize, picSize);
WriteableBitmap mypic = new WriteableBitmap(picSize, picSize);
if (file.Count > 0)
{
foreach (StorageFile f in file)
{
fname = "face" + i + ".jpg";
if (f.Name == fname)
{
i = i + 1;
ImageProperties properties = await f.Properties.GetImagePropertiesAsync();
WriteableBitmap wb = new WriteableBitmap((int)properties.Width, (int)properties.Height);
wb.SetSource((await f.OpenReadAsync()).AsStream());
reSize(wb, wv);
FilterWriteableBitmap(wv, mypic);
img.Source = mypic;
}
}
}
当我尝试直接拍摄照片意味着我写if(f.Name=="face10.jpg")
然后它可以正常工作但在循环内它停在face9。
答案 0 :(得分:2)
更改
int i = 0;
到
int i = 1;
假设该文件夹中有10个文件,foreach将经历10次,但第一次查找face0.jpg并且只能访问face9.jpg。