Use your webcam with the WP7 Emulator (with code!)

August 6, 2010

The GPS Simulator seemed pretty popular with many developers and whilst I was browsing through various forums, I noticed the request for webcam usage as the emulator’s camera. In an earlier project (using the April CTP), I needed to be able to use the webcam as well as the ability to use images from within the application. The latter spawned the Testing Photos with WP7 Emulator post, but I could see no way to use my webcam as the camera. Well now (literally 10 minutes after coming up with the idea – that’s what happens when you have awesome dev tools ;)), I simply re-factored the GPS Sim code and have it working for a webcam instead of Bing maps. What PicSim allows you to do is to use your webcam to capture an image and then immediately have that image streamed over to your WP7 application, as well as other images stored on your computer. You can download the source code and see a screenshot below.

A few things to note:

Firstly, there’s no direct interface to webcams (AFAIK) in .NET. There are various libraries out there that allow you to access the user’s webcam, but I decided to follow this method and use the commonly found avicap32.dll library. This DLL file is found in most System32 folders so hopefully you’ll already have it :). I’ve slightly modified the WebCam class from that site because it was saving the image out to a file instead of a MemoryStream. I also suppressed a popup on line 105 (in case you need to debug) which kept coming up even though the program was running fine.

Secondly, there is no error handling or anything. As I say, it was a quick 10 minute re-factor of the GPS Sim. In order to get it working, simply take the picture (or load one from your computer) and click on the relevant “send image” button. Then, from the WP7 app, simply click on Load Image and hopefully the image will show within the app (as shown by the recursive image below). All the images are converted to JPEG format, but you can change that in the source.

The download has one zip file which contains one solution hosting both projects. You can run both projects from VS2010 for Windows Phone Beta (simply load the solution). The PicSim Consumer app is a WP7 app demonstrating how to get the image, so you can simply apply the same method in your WP7 apps.

Hope this is useful for your development!

Download the source code

Retweet This Post

–@keyboardP