2 out of 2 rated this helpful - Rate this topic

How to: Use the Photo Chooser Task for Windows Phone

Windows Phone

March 23, 2012

Use the photo Chooser task to enable users to select an existing photo from the phone. This task launches the Photo Chooser application. If the user completes the task, an event is raised and the event handler receives a photo in the result.

By using Choosers, you help provide a consistent user experience throughout the Windows Phone platform. For more information, see Launchers and Choosers Overview for Windows Phone.

Important noteImportant Note:

To test the photo Chooser task on a physical device, use the Connect Tool to launch your application. For more information, see How to: Use the Connect Tool for Windows Phone.

To use the photo Chooser task

  1. Add the following statement to your code.

    using Microsoft.Phone.Tasks;
    
  2. Declare the task object. It must have page scope, so declare it in your page before the constructor.

    PhotoChooserTask photoChooserTask;
    
  3. Add the following code to your page constructor. This code initializes the task object, and identifies the method to run after the user completes the task.

    photoChooserTask = new PhotoChooserTask();
    photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
    
  4. Add the following code to your application wherever you need it, such as in a button click event. To test this procedure, you can put the code in the page constructor. This is the code to launch the task.

    try
    {
        photoChooserTask.Show();
    }
    catch (System.InvalidOperationException ex)
    {
        MessageBox.Show("An error occurred.");
    }
    
  5. Add the code for the completed event handler to your page. This code runs after the user completes the task. The result is a PhotoResult object that exposes a stream containing the image data. For information about working with photo image streams, see Camera and Photos for Windows Phone.

    void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            MessageBox.Show(e.ChosenPhoto.Length.ToString());
    
            //Code to display the photo on the page in an image control named myImage.
            //System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
            //bmp.SetSource(e.ChosenPhoto);
            //myImage.Source = bmp;
        }
    }
    

Did you find this helpful?
(1500 characters remaining)