Introduction

Silverlight 4 Beta 1 has been released by Microsoft on 18th November 2009. There are lots of goodies came up with the release of the new version. Among them, most of all are requested by the developers & users of Silverlight. In this post we will demonstrate one of the new feature “Accessing Default Webcam using Silverlight 4”

Pre-Requisite

To create a Silverlight 4 application you need “Visual Studio 2010 Beta 2”. Download it from the Microsoft site. Then install the “Silverlight Tools 4 for Visual Studio 2010 Beta 2”. After successful installation, create a Silverlight 4 Application project.

XAML Steps

Once you done with the project creation, Visual Studio will open the MainPage.xaml for you. Add a Rectangle & three Buttons inside the Grid. The Rectangle will responsible for the Video output from your VideoCaptureDevice & buttons will be responsible for the interaction with the device. After adding the same your XAML will look like this:

 <Grid x:Name=”LayoutRoot” Background=”White”>
          <StackPanel HorizontalAlignment=”Center”>
                <Rectangle x:Name=”rectWebCamView” Width=”500” Height=”400”/>
                <StackPanel Orientation=”Horizontal” HorizontalAlignment=”Center”>
                       <Button x:Name=”btnCaptureDevice” Content=”Capture Device” Margin =”5”/>
                       <Button x:Name=”btnPlayCapture” Content=”Start Capture” Margin=”5”/>
                       <Button x:Name=”btnStopCapture” Content=”Stop Capture” Margin=”5”/>
                </StackPanel>
           </StackPanel>
</Grid>

Code Steps

Now, go to the code behind file (MainPage.xaml.cs) & create an instance of CaptureSource. Then call TryCaptureDevice() to initiate the Video Capture. This first get the default Video Capture device & assign it to the VideoBrush instance of the rectangle. Remember that, this will ask the user to grant permission to the user device & upon successful only it will start the device.

            private void TryCaptureDevice()
            {
                    // Get the default video capture device
                    VideoCaptureDevice videoCaptureDevice =
CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

                    if (videoCaptureDevice == null)
                    {
                          // Default video capture device is not setup
                          btnPlayCapture.IsEnabled = false;
                          btnStopCapture.IsEnabled = false;
                         btnCaptureDevice.IsEnabled = true;

                         MessageBox.Show(“You don’t have any default capture device”);
                    }
                    else
                    {
                         btnPlayCapture.IsEnabled = false;
                         btnStopCapture.IsEnabled = false;

                         // Set the Capture Source to the VideoBrush of the rectangle
                        VideoBrush videoBrush = new VideoBrush();
                        videoBrush.SetSource(captureSource);
                        rectWebCamView.Fill = videoBrush;

                        // Check if the Silverlight has already accessto the device or grant access from the user
                        if (CaptureDeviceConfiguration.AllowedDeviceAccess ||
CaptureDeviceConfiguration.RequestDeviceAccess())
                        {
                                btnPlayCapture.IsEnabled = true;
                                btnStop Capture.IsEnabled = false;
                                btnCaptureDevice.IsEnabled = false;
                        }
                    }
            }

Conclusion

This is a sample application to showcase the new Webcam feature in Silverlight 4. This can be modified to save the image snapshot from the webcam.

What is so SPECIAL on HostForLife.eu Silverlight 4 Hosting?

We know that finding a cheap, reliable web host is not a simple task so we’ve put all the information you need in one place to help you make your decision. At HostForLife, we pride ourselves in our commitment to our customers and want to make sure they have all the details they need before making that big decision.

We will work tirelessly to provide a refreshing and friendly level of customer service. We believe in creativity, innovation, and a competitive spirit in all that we do. We are sound, honest company who feels that business is more than just the bottom line. We consider every business opportunity a chance to engage and interact with our customers and our community. Neither our clients nor our employees are a commodity. They are part of our family.

The followings are the top 10 reasons you should trust your online business and hosting needs to us:

- FREE domain for Life - HostForLife gives you your own free domain name for life with our Professional Hosting Plan and 3 free domains with any of Reseller Hosting Plan! There’s no need to panic about renewing your domain as HostForLife will automatically do this for you to ensure you never lose the all important identity of your site
- 99,9% Uptime Guarantee - HostForLife promises it’s customers 99.9% network uptime! We are so concerned about uptime that we set up our own company to monitor people’s uptime for them called HostForLife Uptime
- 24/7-based Support - We never fall asleep and we run a service that is opening 24/7 a year. Even everyone is on holiday during Easter or Christmast/New Year, we are always behind our desk serving our customers
- Customer Tailored Support - if you compare our hosting plans to others you will see that we are offering a much better deal in every aspect; performance, disk quotas, bandwidth allocation, databases, security, control panel features, e-mail services, real-time stats, and service
- Money Back Guarantee - HostForLife offers a ‘no questions asked’ money back guarantee with all our plans for any cancellations made within the first 30 days of ordering. Our cancellation policy is very simple - if you cancel your account within 30 days of first signing up we will provide you with a full refund
- Experts in Silverlight 4 Hosting
- Given the scale of our environment, we have recruited and developed some of the best talent in the hosting technology that you are using. Our team is strong because of the experience and talents of the individuals who make up HostForLife
- Daily Backup Service - We realise that your website is very important to your business and hence, we never ever forget to create a daily backup. Your database and website are backup every night into a permanent remote tape drive to ensure that they are always safe and secure. The backup is always ready and available anytime you need it
- Easy Site Administration - With our powerful control panel, you can always administer most of your site features easily without even needing to contact for our Support Team. Additionally, you can also install more than 100 FREE applications directly via our Control  Panel in 1 minute!

Happy Hosting!