从本地文件夹获取图像列表

时间:2016-12-28 20:46:20

标签: c# uwp

我正在编写一个C#UWP应用程序,用于显示用户照片库中的图像。我想知道如何获取所有照片并在GridViewItem和flipviewitem中显示每个照片。

private async void Page_Loaded(object sender, RoutedEventArgs e)
{
    var pictureQueryOptions = new QueryOptions();
    //Read through all the subfolders. 
    pictureQueryOptions.FolderDepth = FolderDepth.Deep;
    //Apply the query on the PicturesLibrary 
    var pictureQuery = KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(pictureQueryOptions);
    // 
    var picturesInformation = new FileInformationFactory(pictureQuery, ThumbnailMode.PicturesView);
    picturesSource.Source = picturesInformation.GetVirtualizedFilesVector();
}

public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string culture)
    {
        if (value != null)
        {
            var img = (IRandomAccessStream)value;
            var picture = new BitmapImage();
            picture.SetSource(img);
            return picture;
        }
        return DependencyProperty.UnsetValue;
    }          

    public object ConvertBack(object value, Type targetType, object parameter, string culture)
    {
        throw new NotImplementedException();
    }
}

2 个答案:

答案 0 :(得分:0)

您可以从

获取用户照片库
var myPictures = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Pictures);

循环浏览此文件夹中的所有图片

更新Package.appxmanifest

<Capabilities>
<uap:Capability Name="picturesLibrary" />

答案 1 :(得分:0)

对您的代码最简单的修复方法是使用此构造函数增加缩略图的大小(它们可以达到1024px,因此它应该足够了):

FileInformationFactory.FileInformationFactory(IStorageQueryResultBase, ThumbnailMode, UInt32, ThumbnailOptions)

除此之外,您可以从StorageFile设置BitmapImage,如下所示:https://social.msdn.microsoft.com/Forums/en-US/bc9aa758-f211-468b-8168-a671b7a0c8b4/uwp-creating-bitmapimage-with-uri-accessing-file-from-same-uri-gives-exception?forum=wpdevelop