📜 ⬆️ ⬇️

Create an application for Windows Phone 7 from start to finish. Part 15. Access to photos on Windows Phone

Previous part

You can use photos and other images as application resources to display them in the user interface. Your application can access the photos in the phone’s media library or allow users to take pictures using the built-in camera.

In this part you will learn:

Access photos on Windows Phone


To access images from the media library and camera images, use PhotoChooserTask and CameraCaptureTask. These classes are known as choosers , because they open a separate window in which the user makes a choice, which is then returned to your application. There are also launchers (launch), which open a separate window, but do not return the user's choice to the application.
')
PhotoChooserTask and CameraCaptureTask require similar code, so we only describe PhotoChooserTask. In addition, it will be described how to save selected photos to isolated storage. You can also receive and save images to the media library through the XNA MediaLibrary class. However, this class will not be considered.

To use a chooser, you need to create an instance of it. The following image shows an example of PhotoChooserTask.

image

After the user has made a selection, you must process the Completed event to perform the appropriate actions. The following code snippet (from CarDetailsPage.xaml.cs) demonstrates how to create a PhotoChooserTask and handle its Completed event. Note that PhotoChooserTask must be a global variable for the page, and the Completed event must be enabled in the constructor.
private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  1. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  2. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  3. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  4. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  5. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  6. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  7. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  8. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  9. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  10. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  11. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  12. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  13. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  14. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  15. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  16. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  17. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  18. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  19. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  20. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  21. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  22. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  23. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  24. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  25. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  26. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
  27. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .
private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .

After the user clicks the “Photo” button, PhotoChooserTask is displayed. This causes deactivation of the application. After the user has selected a photo, the application is reactivated, although in this case, the PhotoChooserTask.Completed event occurs before the OnNavigatedTo method is called to handle the reactivation.

The Completed event handler creates a new BitmapImage and sets the selected photo as its source. Next, the event handler puts the image into isolated storage. The images are not serializable, so they cannot be stored in the state dictionaries stored in the memory, nor in the state dictionaries of isolated storage. Therefore, if you want to save an image, you must save it using the isolated storage virtual file system interface. The CarDataStore class should also put the photo into isolated storage after the user clicks the Save button. Thus, the SaveTempCarPhoto and GetTempCarPhoto methods for processing a temporary photo simply wrap the SaveCarPhoto and GetCarPhoto methods used to process the saved photo. The description of the SaveCarPhoto and GetCarPhoto methods was given in the section “Isolated storage: saving and loading data”.

And at the end, the event handler assigns an image to the Picture property of the Car object stored in the car field. After that, the photo will appear in the Image control associated with the Picture property.

Note:
The PhotoResult class, passed to the Completed event handler, contains the OriginalFileName property. This property would be convenient to use to retrieve a selected photo from the XNA MediaLibrary so that you do not need to save it separately in isolated storage. However, at the time of this writing, the implementation of OriginalFileName behaves not as expected: it returns a temporary file name, which is not useful for getting photos from the XNA MediaLibrary class later. However, even if you could use the media library in this way, you would most likely still prefer to store user-selected images in isolated storage. In this case, they will remain accessible to your application, even if the user later deletes the images from the media library or changes their content using other applications.

Source: https://habr.com/ru/post/118462/


All Articles