Silverlight 4 webcam feature

silverlight-4-webcam

shown on Google Chrome

I was looking for an easy project to try my new Visual Studio 2010 Beta 2, so I decided to install the Silverlight 4 Tools and copy pasted the tutorial that I found on Ronald Rajagukguk's blog, a devaloper evangelist from Microsoft indonesia, And got the code working in less than 2 mins.

So here's the snippet if you're interested to see what it takes to capture video stream from the webcam.

// check permission
if ( CaptureDeviceConfiguration.AllowedDeviceAccess ||
    CaptureDeviceConfiguration.RequestDeviceAccess() )
{
    // a static helper class to get the default device.
    // very handy!
    // unfortunately it will also makes it hard for
    // mocking it out when practising TDD
    var video = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

    var audio = CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice();

    if ( null != video && null != audio )
    {
        // specify a capture source and
        // feed our capture devices
        var source = new CaptureSource();
        source.VideoCaptureDevice = video;
        source.AudioCaptureDevice = audio;

        // start capturing
        source.Start();

        // create a video brush
        var videoBrush = new VideoBrush();
        videoBrush.Stretch = Stretch.Fill;
        videoBrush.SetSource( source );

        // bind it to a xaml element in this case
        // a rectangle
        Rec.Fill = videoBrush;
    }
    else
    {
        MessageBox.Show( "Error." );
    }
}

I use: