FilePicker represents a control that allows a user to pick a file.
Step 1: Create a Blank Windows 10 Universal App (C#) and enter App Name.
Step 2:
Next add one Button and two Textblock controls for displaying information from the toolbox then change the button name to SelectAPicture and TextBlock2 Name: OutputTextBlock.
Step 3: Add the following namespace.
- using System.Threading.Tasks;
- using Windows.Storage.Pickers;
- using Windows.Storage;
Copy and paste the following code to the SelectAPicture Button event in the cs page.
- private async void SelectAPicture _Click(object sender, RoutedEventArgs e)
- {
- FileOpenPicker openPicker = new FileOpenPicker();
- openPicker.ViewMode = PickerViewMode.Thumbnail;
- openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
- openPicker.FileTypeFilter.Add(".jpg");
- openPicker.FileTypeFilter.Add(".jpeg");
- openPicker.FileTypeFilter.Add(".png");
- StorageFile file = await openPicker.PickSingleFileAsync();
- if (file != null)
- {
-
- OutputTextBlock.Text = "Selected Photo:" + file.Name;
- }
- else
- {
- OutputTextBlock.Text = "Operation cancelled.";
- }
- }
Step 4: Run your application and select a picture, It will be shown in our
OutputTextBlock.
Read more articles on Window 10: