How to use the camera capture task for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Use the camera capture task to enable users to take a photo from your application using the built-in Camera application. If the user completes the task, an event is raised and the event handler receives a photo in the result. On Windows Phone 8, if the user accepts a photo taken with the camera capture task, the photo is automatically saved to the phone’s camera roll. On previous versions of Windows Phone, the photo is not automatically saved.

Important Note:

Photos captured with the CameraCaptureTask API are always copied to the phone’s camera roll. If the customer has set up his or her phone for automatic uploads, these pictures will be copied to OneDrive and potentially shared with a broader audience than intended by your app. For this reason, if your app captures pictures that you explicitly do not want to be shared or uploaded, such as temporary images or images that contain private information, do not use the CameraCaptureTask API. Instead, you should use the PhotoCamera API to implement your own camera UI. For more information on creating camera apps, see How to create a base camera app for Windows Phone 8.

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

Memory allocated for the Camera Capture Task does not count toward total application memory use. This helps minimize the amount of memory your application uses to capture photos, which is particularly important when your application runs on a lower-memory device. For more information, see Developing apps for lower-memory phones for Windows Phone 8.

When testing the camera capture task using the emulator, press F7 while the task is active to capture a photo.

To use the camera capture task

  1. Add the following statement to your code.

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

    CameraCaptureTask cameraCaptureTask;
    
    Dim cameraCaptureTask As CameraCaptureTask
    
  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.

    cameraCaptureTask = new CameraCaptureTask();
    cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
    
    cameraCaptureTask = new CameraCaptureTask()
    AddHandler cameraCaptureTask.Completed, AddressOf cameraCaptureTask_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.

    
    
    
    cameraCaptureTask.Show();
    
    
    
    
    
    
    cameraCaptureTask.Show()
    
    
    
    
    
  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 8.

    void cameraCaptureTask_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;
        }
    }
    
    Private Sub cameraCaptureTask_Completed(sender As Object, e As PhotoResult)
    
        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.
            'Dim bmp as System.Windows.Media.Imaging.BitmapImage = new System.Windows.Media.Imaging.BitmapImage()
            'bmp.SetSource(e.ChosenPhoto)
            'myImage.Source = bmp
        End If
    End Sub
    

See Also

Reference

CameraCaptureTask

Completed

Other Resources

How to use the photo chooser task for Windows Phone 8