Using File Picker in Windows 10

A file picker represents a UI element that lets the user choose and open files.

Step 1: Open a blank app and add a Button and a Image control either from the toolbox or by copying the following XAML code into your grid.

  1. <StackPanel Margin="10,40,0,0">  
  2.    <TextBlock Text="File Picker" FontSize="20"></TextBlock>  
  3.    <Button Name="imageOpenButton" Content="Open Image" Height="40" Width="120" Click="imageOpenButton_Click"></Button>  
  4.    <Image Margin="0,20,0,0" Name="selectedImage" Height="300" Width="300"></Image>  
  5. </StackPanel>  

Step 2 : Add the following namespaces to your project which is needed in further C# code:

  1. using Windows.Storage;  
  2. using Windows.Storage.Pickers;  
  3. using Windows.Storage.Streams;  
  4. using Windows.UI.Xaml.Media.Imaging;  

Step 3: Copy and paste the following code to the cs page which will be called on button clicking event and will start the file picker and the selected image will be displayed.

  1. FileOpenPicker openPicker = new FileOpenPicker();  
  2. openPicker.ViewMode = PickerViewMode.Thumbnail;  
  3. openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;  
  4. openPicker.FileTypeFilter.Add(".jpg");  
  5. openPicker.FileTypeFilter.Add(".jpeg");  
  6. openPicker.FileTypeFilter.Add(".png");  
  7. StorageFile image = await openPicker.PickSingleFileAsync();  
  8. BitmapImage bitmapImage = new BitmapImage();  
  9. using(IRandomAccessStream fileStream = await image.OpenAsync(FileAccessMode.Read))  
  10. {  
  11.     bitmapImage.SetSource(fileStream);  
  12. }  
  13. selectedImage.Source = bitmapImage;  


Step 4: Run your application and test yourself.


Up Next
    Ebook Download
    View all
    View all