Yes Silverlight 4 is out, so is Visual Studio 2010, TFS 2010, SharePoint 2010, SQL 2008 R2, Office 2010 and the list goes on and on. I wont post anything related to new releases anymore as I don’t run a technology news site. So all my posts from now on will be raw features in the latest “toys” (OK old ones too). Now that I got that out of my system, time to move on to the actual article.

  Silverlight 4 allows you to access the webcam and microphone on a client machine. This can find lots of applications but I guess it will be used mostly for web conferencing and VOIP applications. The code is very simple; My XAML page contains a button that will start the video capture and a rectangle on which the video will be displayed. Now I am not a fan of the rectangle and really wanted to use the MediaElement instead but no luck. So this technique attempts to access the web cam on the client machine, set the video from the web cam as the source of a VideoBrush, and then fills the rectangle with that VideoBrush;

 

//Get default capture device as configured on the client

VideoCaptureDevice webcam = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

//attempt to access the deive

if (CaptureDeviceConfiguration.RequestDeviceAccess())

{

//create a source

CaptureSource source = new CaptureSource { VideoCaptureDevice = webcam };

//create the video brush

VideoBrush videoBrush = new VideoBrush { Stretch = Stretch.Uniform };

videoBrush.SetSource(source);

//start capture

source.Start();

//fill rectangle with video brush

video.Fill = videoBrush;

}

Advertisements