Wednesday, March 03, 2010

Webcam access in Silverlight 4

Code snippet to show webcam capture in silverlight 4


CaptureDeviceConfiguration.RequestDeviceAccess();
CaptureSource _capture = new CaptureSource();
_capture.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
_capture.Start();
VideoBrush videoBrush = new VideoBrush();
videoBrush.Stretch = Stretch.Uniform;
videoBrush.SetSource(_capture);
rectVideo.Fill = videoBrush;
rectVideo.UpdateLayout();


Getting the output and resizing it to have a format of a picture that can be used in a profile


_capture.AsyncCaptureImage(img =>
{
_capture.Stop();
TranslateTransform tt = new TranslateTransform();

tt.Y = -73.5;
tt.X = -125;
WriteableBitmap bitmap = new WriteableBitmap(70, 93);

bitmap.Render(rectVideo, tt);
bitmap.Invalidate();
frameImage.Source = bitmap;
_capture.Start();
});