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:
- Capture Camera (StartCameraTask)
- Choose Email Address (EmailAddressChooserTask)
- Compose Email (EmailComposeTask)
- Open Market Place (MarketplaceLauncher)
- Open Media Player (MediaPlayerLauncher)
- Make Phone Call (PhoneCallTask)
- Pick Phone Number (PhoneNumberChooserTask)
- Choose Photo (PhotoChooserTask)
- Save Email (SaveEmailAddressTask)
- Save Phone Number (SavePhoneNumberTask)
- Search (SearchTask)
- Compose SMS (SmsComposeTask)
- Browse Web (WebBrowserTask)
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…
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’.
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
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;
};
}
}
}
my visual studio express 2010 for windows phone version is 30319.31
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?
Did you setup the capabilities file ? See this link for more info: http://blogs.msdn.com/b/jaimer/archive/2010/04/30/windows-phone-capabilities-security-model.aspx
Can you share your WMAppManifest.xml file?
I follow the capabilities blog but still see a black screen.
I have all the capabilities. Still black screen.
atonical, are you using vista?
I bought a new laptop and switch to windows 7, now I see what is shown in this blog….
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!
gar,
Are you sure you’re using the latest version of the tools ?
Jeremy,
Yes.
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!
I’ve seen your question on the WP7 forum. This is weired, I have no idea what is wrong 🙁
Jeremy,
Thank you. I got the black screen on vista, and I will try it on OS 7 later.
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();
}
After updating my graphic card drivers I saw some extra buttons but none of them takes a picture.
You can follow this issue at http://www.windowsphone7.com/backstage/forums/index.aspx/gaming-app-development_applications_pciture-w-camera-emulator
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
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);
t.Completed += new EventHandler
void t_Completed(object sender, PhotoResult e)
{
throw new NotImplementedException();
}
Hope it helps 🙂
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!!
hi, try this out it work Capturing Photo by Windows 7 phone Camera
http://madhukaudantha.blogspot.com/2010/09/capturing-photo-by-windows-7-phone.html