Windows Phone

Learn Windows Phone 7 Mango camera fundamentals

Justin James details the basic components of the camera system on Windows Phone 7 Mango that WP7 developers need to know.

The Windows Phone 7 Mango release (Windows Phone 7.1) is now available on many devices. In a nice change of pace, it looks like the rollout is going smoothly and rapidly to a wide variety of devices. While Mango has a pile of great new features, I want to take a closer look at the camera system.

What you need to know to get started

In WP7.0, the camera functionality was extremely limited. You could request that the user take a picture or select a photo from the Pictures hub. The idea was to keep things as secure as possible, and Microsoft clearly chose to not deliver a feature than release one that didn't work really well. With Mango, the camera is much more full featured. Developers can now control the camera directly, and send commands to it like "take a picture" and adjust the flash settings. In addition, it is possible to directly capture video and audio from the camera and microphone and work with it in real time, meaning that augmented reality applications are now possible.

The basic class to work with the camera is PhotoCamera. With it, you can tell it to start or cancel focusing, retrieve what the viewfinder shows, set the flash mode and camera resolution, and take a picture. When you are creating your instance of PhoneCamera, you will want to be a little careful how you do it. You will want to ensure that there actually is a camera, and that it is the kind you want (front or rear facing). There is an enumeration called Camera Type that represents the kinds of cameras (CameraType.FrontFacing and CameraType.Primary); you can pass these to the PhoneCamera.IsCameraTypeSupported() method to find out which are available. Once you determine which camera you can use, pass the appropriate CameraType value to the constructor of the PhotoCamera class, and your instance will be tied to the correct camera.

The camera operations are asynchronous. For example, you tell it to start taking a picture (CaptureImage()), and that it is a non-blocking call, and an event will be fired (CaptureImageAvailable) notifying you that the picture capture is complete. The same is true for focusing (Focus(), AutoFocusCompleted). Also, the camera will generate a thumbnail image of the picture, which you can use to represent the picture, and that thumbnail generation fires the CaptureThumbnailAvailable event. The camera button events (ShutterKeyHalfPressed, ShutterKeyPressed, and ShutterKeyReleased) are also available if you want to work with them. The documentation makes it clear that you should not handle the phone-related events unless it is absolutely necessary. Instead, use the OnNavigatedTo event of the page to add the event handlers, and the OnNavigatedFrom event to remove those event handlers.

Once you grab the image, you can save it to the Isolated Storage system, or you can put it into the Camera Roll of the phone, letting it show up under the Pictures hub. The CaptureImageAvailable event will come with a ContentReadEventArgs object. The main member of this class is the ImageStream property, which is a Stream object representing the picture. To save the picture to the Camera Roll, instantiate an instance of the MediaLibrary class, then call the SavePictureToCameraRoll() method, passing it the ImageStream property from the event argument. Alternatively, the SavePicture() method will put it under "Saved Pictures" in the Pictures hub.

To use the camera as a viewfinder, you need to place a Canvas tag in your XAML to provide a surface for the camera's preview. In the Canvas tag, you will need to have a Background tag, with a VideoBrush tag within it. Make sure that you give the VideoBrush element a name. Then in your code, you will use the SetSource() method of the VideoBrush and pass in your instance of the PhoneCamera that you are using.


These are the basic components of the camera system on Windows Phone 7 Mango. By combining these tips with your own logic and interface, you can do all sorts of tricks, especially when combined with other components of the system and social media integrations.



Justin James is the Lead Architect for Conigent.


Editor's Picks