File Picker In Windows 10 UWP

In Windows 10 we have OpenFileDialog which opens a dialog to pick the files. Here we are going to pick the image file in Windows 10.

Create new Windows 10 project and create a button and image control to perform File Picker operation.

Firstly, create a new instance for FileOpenPicker as in the following snippet:

  1. FileOpenPicker openPicker = new FileOpenPicker();  
Next, we have to set file picker ViewMode, you could ignore this, but if you would like to add, there are two options, such as List and Thumbnail. Here I will set thumbnail.
  1. openPicker.ViewMode = PickerViewMode.Thumbnail;  
Then set the suggested location as you wish. I am going to set default location as picture library.
  1. openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;  
We need to add FileTypeFilter; it is a mandatory fields that you should add. It requires at least one type.
 
The FileTypeFilter is a readonly collection. We would be able to add values.

Here I am setting filter type for png and jpg images.
  1. openPicker.FileTypeFilter.Add(".jpg");  
  2. openPicker.FileTypeFilter.Add(".png");  
Finally, we are going to show the file picker dialog as in the following code snippet and we have the option to select single or multiple file selection

For single file selection,
  1. StorageFile file = await openPicker.PickSingleFileAsync();  
For multiple file selection,
  1. StorageFile file = await openPicker.PickMultipleFilesAsync();  
Finally, we are going to set the selected image stream to empty image control.
  1. if (file != null)  
  2. {  
  3.     var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);  
  4.     var image = new BitmapImage();  
  5.     image.SetSource(stream);  
  6.     imageView.Source = image;  
  7. }  
  8. else  
  9. {  
  10.     //  
  11. }  
The whole code looks like the following:
  1. private async void openBtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     FileOpenPicker openPicker = new FileOpenPicker();  
  4.     openPicker.ViewMode = PickerViewMode.Thumbnail;  
  5.     openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;  
  6.     openPicker.FileTypeFilter.Add(".jpg");  
  7.     openPicker.FileTypeFilter.Add(".png");  
  8.     StorageFile file = await openPicker.PickSingleFileAsync();  
  9.     if (file != null)  
  10.     {  
  11.         var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);  
  12.         var image = new BitmapImage();  
  13.         image.SetSource(stream);  
  14.         imageView.Source = image;  
  15.     }  
  16.     else  
  17.     {  
  18.         //  
  19.     }  
  20. }  
For multiple file selection
  1. foreach(var files in filelist)  
  2. {  
  3.    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);  
  4.    var image = new BitmapImage();  
  5.    image.SetSource(stream);  
  6.    yourimagelist.Add(image);  
  7. }  
Finally, run your app and you can see the following output:

 run your app

Open file

Source Code.