Silverlight webcam API throws a strange error

I am trying to show my webcam using VideoBrush on a rectangle. However, I ran into this strange problem: -

External component has thrown an exception.

   at MS.Internal.XcpImports.CaptureGraph_GetAvailableVideoCaptureDevicesNative(IntPtr pContext, Int32& typeIndex, CValue& DeviceCollection)
   at MS.Internal.XcpImports.CaptureGraph_GetAvailableVideoCaptureDevices()
   at System.Windows.Media.CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice()
   at System.Windows.Media.CaptureSource..ctor()
   at FMT.Client.Views.ChildWindows.Webcam.Webcam_Loaded(Object sender, RoutedEventArgs e)

Then I right-clicked on the Silverlight application, went into the configuration, and then on the Mic / Webcam tab. I have a webcam selected there, and I see that my webcam is excellent when I am in this dialog box. Here is my code: -

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System;

namespace FMT.Client.Views.ChildWindows
{
    public partial class Webcam : ChildWindow
    {
        private CaptureSource _source;
        private CaptureDevice _webcam;
        private VideoBrush _webcamBrush;
        private ImageBrush _capturedImage;

        public Webcam()
        {
            InitializeComponent();
            this.Loaded += Webcam_Loaded;
        }

        void Webcam_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //if (CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices().Count <= 0)
                //{
                //    ErrorWindow window = new ErrorWindow("Error","Please check the default webcam in the Silverlight config. You can specify the video and audio devices that are used by default with the Silverlight Configuration. Just press the right mouse button over the application, click Silverlight in the context menu and select the  Webcam / Mic tab to set them.");
                //    window.Show();
                //    this.DialogResult = false;
                //}
                //else
                //{
                    _source = new CaptureSource();
                    _source.CaptureImageCompleted += _source_CaptureImageCompleted;
                    _source.CaptureFailed += _source_CaptureFailed;
                    _webcam = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
                    if (_webcam != null)
                    {
                        _source.VideoCaptureDevice = (VideoCaptureDevice)_webcam;
                        _webcamBrush = new VideoBrush();
                        _webcamBrush.SetSource(_source);
                        capturedRect.Fill = _webcamBrush;
                        _capturedImage = new ImageBrush();
                        RequestAccess();
                    }
                //}
            }
            catch (Exception ex)
            {
                ErrorWindow window = new ErrorWindow(ex);
                window.Show();
                this.DialogResult = false;
            }
        }

        void RequestAccess()
        {
            if (CaptureDeviceConfiguration.RequestDeviceAccess() && _source.VideoCaptureDevice != null)
            {
                _source.Start();
            }
            else
                this.DialogResult = false;
        }

        void _source_CaptureFailed(object sender, ExceptionRoutedEventArgs e)
        {
            ErrorWindow window = new ErrorWindow(e.ErrorException);
            window.Show();
        }

        void _source_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
        {
            if(e.Result != null)
                _capturedImage.ImageSource = e.Result;
        }

        private void hypCapture_Click(object sender, RoutedEventArgs e)
        {
            if (_source.VideoCaptureDevice != null &&
               _source.State == CaptureState.Started)
            {
                _source.CaptureImageAsync();
            }
        }

    }
}

Any idea why the execution is not executed when the CaptureSource constructor is called?

Thanks in advance:)

+3
source share

All Articles