locked
using query.GetFilesAsync to load image files RRS feed

  • Question

  • All,

    I am looking to load all the image files in a directory. The below statement is what I am using to access the folder.

     var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".jpg" });

                var query = folder.CreateFileQueryWithOptions(queryOptions);

                var files = await query.GetFilesAsync();

             

    The below statement is what I am using to display the images.

    itemsViewSource.Source = imageList;

    I am lost on how to access the files variable to format it so I can load it to the itemsViewSource.Source variable.

    Thanks,

    Grim

    Wednesday, December 11, 2013 8:10 PM

Answers

  • The files variable is a list of StorageFiles. You can see this by hovering over your "var" in Visual Studio or by looking up StorageFileQueryResult.GetFilesAsync in the documentation.

    Your data template probably doesn't know how to use StorageFiles directly. What exactly it does do depends on your template. If you're ultimately trying to put these into Images you'll need to open the file and load it into a BitmapImage.

    Assuming your object is databound to an ObservableCollection<BitmapImage> myImages:

              var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".jpg" });
               var query = folder.CreateFileQueryWithOptions(queryOptions);
               var files = await query.GetFilesAsync();
               foreach (var f in files)
               {
                   BitmapImage bmp = new BitmapImage();
                   bmp.SetSource(await f.OpenAsync(FileAccessMode.Read));
                   myImages.Add(bmp);
               }
    

    --Rob

    • Marked as answer by A.B.P.Lambert Thursday, December 12, 2013 5:46 PM
    Thursday, December 12, 2013 3:32 AM
    Moderator

All replies

  • The files variable is a list of StorageFiles. You can see this by hovering over your "var" in Visual Studio or by looking up StorageFileQueryResult.GetFilesAsync in the documentation.

    Your data template probably doesn't know how to use StorageFiles directly. What exactly it does do depends on your template. If you're ultimately trying to put these into Images you'll need to open the file and load it into a BitmapImage.

    Assuming your object is databound to an ObservableCollection<BitmapImage> myImages:

              var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".jpg" });
               var query = folder.CreateFileQueryWithOptions(queryOptions);
               var files = await query.GetFilesAsync();
               foreach (var f in files)
               {
                   BitmapImage bmp = new BitmapImage();
                   bmp.SetSource(await f.OpenAsync(FileAccessMode.Read));
                   myImages.Add(bmp);
               }
    

    --Rob

    • Marked as answer by A.B.P.Lambert Thursday, December 12, 2013 5:46 PM
    Thursday, December 12, 2013 3:32 AM
    Moderator
  • Thanks!!!
    Thursday, December 12, 2013 5:47 PM