[WP7] Using the camera in the emulator

In the very first release of the SDK for Windows Phone 7 development, it was not possible to use the camera in the emulator. The latest version of the SDK fixes this problem.

Windows Phone 7 SDK comes with a set of Task (in the Microsoft.Phone.Tasks namespace). A task can be launched from your application in order to perform some work. Currently available tasks are:

In order to launch a task from your application, all you need to do is to instantiate the associated type and call the Show() method.

Here is a sample code which launchs the StartCameraTask and then gets the capture images in order to use it in a standard Silverlight Image control:

// launch the camera capture when the user touch the screen
this.MouseLeftButtonUp += (s, e) => new CameraCaptureTask().Show();

// this static event is raised when a task completes its job
ChooserListener.ChooserCompleted += (s, e) =>
{
    var taskEventArgs = (TaskEventArgs)e;
    var photoStream = taskEventArgs.Result.ChosenPhoto;

    var bitmapImage = new BitmapImage();
    bitmapImage.SetSource(photoStream);

    this.image.Source = bitmapImage;
};

The image is just a standard Silverlight Image control:


The emulator while the task is running:

The captured image (shown once the task has completed):

If you haven’t download the tool already, go ahead and grab them ! Everything is available for free in a single download at http://developer.windowsphone.com/windows-phone-7-series/.

Hope this helps 🙂

Note: even though the StartCameraTask is now working, this is not yet the case for all the tasks…

21 thoughts on “[WP7] Using the camera in the emulator

  1. any idea why I get this error:

    The type ‘Microsoft.Phone.Tasks.IChooser’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘Microsoft.Phone, Version=2.0.5.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e’.

  2. Shawn,

    As the error message says, IChooser is defined in the Microsoft.Phone assembly which is not included in your project. Right click the Reference item in your project and add a new reference to Microsoft.Phone.dll

  3. Is there any extra steps to make the shutter release button avaliable? I always see a blank screen.

    I copy/pasted the code to a new windows phone application.
    After I dobule clicked on the simulated screen, nothing but a black screen showed up, there was not shutter release button. I clicked, double clicked, and right clicked the blank screen, and nothing happend.

    My code is like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Tasks;
    using System.Windows.Media.Imaging;

    namespace WindowsPhoneApplication2
    {
    public partial class MainPage : PhoneApplicationPage
    {
    public MainPage()
    {
    InitializeComponent();

    SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

    // launch the camera capture when the user touch the screen
    this.MouseLeftButtonUp += (s, e) => new CameraCaptureTask().Show();

    // this static event is raised when a task completes its job
    ChooserListener.ChooserCompleted += (s, e) =>
    {
    var taskEventArgs = (TaskEventArgs)e;
    var photoStream = taskEventArgs.Result.ChosenPhoto;

    var bitmapImage = new BitmapImage();
    bitmapImage.SetSource(photoStream);

    this.image.Source = bitmapImage;
    };
    }
    }
    }

  4. Hi,

    I tried launching the camera task but i just get a black screen. Not like the red screen u shown above. I don’t have the “camera” icon to snap the picture too. Is there anything else that I need to do?

  5. Can you share your WMAppManifest.xml file?
    I follow the capabilities blog but still see a black screen.

  6. atonical, are you using vista?
    I bought a new laptop and switch to windows 7, now I see what is shown in this blog….

  7. I got this error:

    ‘WindowsPhoneApplication1.CameraCaptureTask’ does not contain a definition for ‘Show’ and no extension method ‘Show’ accepting a first argument of type ‘WindowsPhoneApplication1.CameraCaptureTask’ could be found (are you missing a using directive or an assembly reference?)

    There are 4 methods available, it’s very different from the webpage (http://msdn.microsoft.com/en-us/library/microsoft.phone.tasks.cameracapturetask_members%28v=VS.92%29.aspx)

    you can see them by click the following link, and the references are in the image as well.

    http://i45.tinypic.com/otofbk.jpg

    Thanks!

  8. I got the SDK 7.1,

    Microsoft Visual Studio 2010
    Version WM7ToolsRel – 30319.31
    Microsoft .NET Framework
    Version 4.0.30319 RTMRel

    Installed Version: PD Express

    Microsoft Visual C# 2010 02053-315-4422943-70001
    Microsoft Visual C# 2010

    Microsoft XNA Game Studio 4.0
    Microsoft XNA Game Studio 4.0
    Build 4.0.20410.0

    I am not sure are they the latest versions?

    Thanks!

  9. Jeremy,

    Thank you. I got the black screen on vista, and I will try it on OS 7 later.

  10. Thanks, good post!
    I downloaded beta tools on vista and i have the same problem with camera. I see a black screen and the icon that is also seen on the first screenshot (top left, a sitting dog i guess)
    Will post if i find a solution 🙂

    btw here is code for beta tools

    private CameraCaptureTask cameraTask;
    // Constructor
    public MainPage()
    {
    InitializeComponent();
    cameraTask = new CameraCaptureTask();
    cameraTask.Completed += new EventHandler(cameraTask_Completed);
    }

    void cameraTask_Completed(object sender, PhotoResult e)
    {
    var photoStream = e.ChosenPhoto;

    var bitmapImage = new BitmapImage();
    bitmapImage.SetSource(photoStream);

    this.image.Source = bitmapImage;
    }

    private void button1_Click(object sender, RoutedEventArgs ee)
    {
    cameraTask.Show();
    }

  11. Hi Cedric,

    I have the beta tools, and tried the code.
    However I am getting the following error:

    No overload for ‘cameraTask_Completed’ matches delegate ‘System.EventHandler’

    Can you help me?

    Thanks
    Nuno

  12. Nuno,

    In the latest version of the tools, the signature of the event has changed. You must now use:

    var t = new CameraCaptureTask();
    t.Completed += new EventHandler(t_Completed);

    void t_Completed(object sender, PhotoResult e)
    {
    throw new NotImplementedException();
    }

    Hope it helps 🙂

  13. Thanks for this post!!

    I’ve been trying to do it by myself but when application shows the camera interface… application debug stops… and completed event never raises (I think so… I can’t debug it…).

    Do you know why could this be???

    Thanks in advance!!

Leave a Reply

Your email address will not be published. Required fields are marked *