Capturing images through webcams in a WPF app


The Need

I was setting up my Dad’s account on my laptop today when Win 7 provided me with the inane out-of-the box images it cans by default. I immediately thought of capturing an image using the webcam but realized unlike OSX, Win 7 doesn’t have such a utility available. Most laptop manufacturers provide horrendously bloated software to do such type of things but my ‘bootcamped’ Windows 7 setup doesn’t have any of that.

The Options

The nerd that I am, I wanted to ‘roll my own’ immediately. I had seen Silverlight now has webcam support built-in so if Silverlight can do it you can do the same in WPF right? Dead Wrong! Silverlight runtime uses System.Windows.dll from the Silverlight SDK whereas WPF uses System.Windows from the .NET SDK. Unfortunately the .NET SDK doesn’t have direct support for webcams built in, you’ve to work on it. So I googled it up (with Bing, no jokes, ever since I started using IE9 beta I’ve let Bing remain the default search engine). The options presented were:

1. WPF Mediakit from CodePlex : http://wpfmediakit.codeplex.com/ First glance gave the impression it was too heavy for my needs

2. Jon Preece’s blog: http://www.jpreece.com/wpf/webcam-image-capture/ Seemed just right. A little investigation on what ImagingDevice.dll actually does convinced me to go for it using Jon’s code.

3. Scott Hanselman’s Channel 9 article: http://channel9.msdn.com/coding4fun/articles/Look-at-me-Windows-Image-Acquisition Actually I found this article after I was done manipulating Jon’s code and coming up with the solution below. It also uses Interops so it’s not a pure .NET solution either.

The Solution – Windows Picture Booth (pre alpha)

Now when I say solution. I mean, the path I took.

Actually the code is pretty simple. I wrapped Jon’s code in a WPF User Control (WebcamCapture.xaml) and added three methods to Start/Stop and Capture current frame.

The WebcamCapture control is hosted on a WPF Form (MainWindow) that has the following control

Button : Start Camera – Does what it says, calls the StartCamera method in the WebcamCapture control.

Button: Stop Camera – Calls the StopCamera method in WebcamCapture control and stops the camera. More like pauses it, because I’ve not cleaned up the last image captured.

Button: Capture Image – Calls the GrabImage method in the WebcamCapture control and shows it adjacent to the web-cam window.

WebcamCapture Control: Shows the feed coming through the web-cam.

Image Control: Shows the last captured image.

TODO: As mentioned above, this is a pre-alpha and right now I am too sleepy to add much else to it. It doesn’t have too much error handling either so expect it to crash an burn on slightest pretext.

But I really want to make this into the OSX equivalent of PictureBooth.

So keep an eye out for the updates.

You can download the entire code from here

Licensing

The code is being made available under GPL. I am still investigating ImagingDevice.dll licensing. I might have to remove it from the download package in future if it infringes someone else’s copyright. From what I’ve read, it’s supposed to come with Windows.

Advertisements
Tagged , , , ,

16 thoughts on “Capturing images through webcams in a WPF app

  1. lest says:

    I’m working with a tablet in Windows 7. It’s got a webcam (front) and a camera (back). When I’ve integrated the PictureBooth project inside another big project, it FAILS when you use both cameras.

    – When I click a determined button, a new window that contains the PictureBooth is opened. If one of two cameras has been disabled, it works correctly. On the other hand, with two cameras enabled, when you open three or more times the window with the PictureBooth, it doesn’t work correctly.

    – Could I specify to ImagingDevice the camera I want to use?

    • Sumit says:

      Hi,
      Let me try and reproduce it on my laptop with two webcams. I’ll see if I can dig up something to pass the camera handle.
      Thanks and Regards,
      Sumit.

  2. PeterCH says:

    Compiled on VS2010, it works well on Win 7 / 64bits !
    Do you have the src for ImagingDevice.dll ? I’d prefer to use source codes than refer to something I can’t debug.

  3. Josiah Bradbury says:

    Hello! I know this is probably digging up the past, but I have a quick question. where is the dialog box for picking the camera located? is that within the ImagingDevice.dll? I am trying to use your code as an example and I need to be able to progmatically choose the device rather than choose it from the dialog. Or at least make the dialog bigger for a touch device.

    thanks!

    • Sumit says:

      Hi Josiah,
      I am assuming you are getting a dialog box to pick camera because you have more than one camera attached. So your assumption is correct the dialog is indeed coming from the ImagingDevice.dll.

      I have not implemented anything to pick a camera in my code.

      Hope this helps,
      Sumit.

  4. john says:

    When I click the link to get your code, it fails with the message “This item won’t load right now.”

    • Sumit says:

      Hi John,

      My bad. Had a Skydrive mess up last month resulting in URL meltdown. I have updated the link, you should be able to download it now.

      Note to self, upload all code to GitHub 🙂

      Regards,
      Sumit.

  5. DaliborS says:

    Hi Sumit,
    I have downloaded the PictureBooth and built/executed in VS2013. It has worked fine on my All-In-One PC, until I have Stopped the camera via the button. Since then it will always allow me to select the camera and then it displays some picture or icon. When I have deleted the project, restarted the PC and loaded the project again, it would work again once.. Any clues?

  6. raj says:

    Hi Sumit, seems to be link is broken. Since, Skydrive is updated to OneDrive link is not worlking for source code.

    Regards,
    Raj.

  7. Alen says:

    Hi. Can you post new link for demo project?

  8. peugas says:

    Link is down, can you repost?
    Thanks in advance

  9. Sumit says:

    Apologies for the delay in updating the link. It should be accessible again.
    Cheers.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: