Silverlight网络摄像头API抛出奇怪的错误

时间:2011-03-12 18:09:52

标签: c# silverlight silverlight-4.0 webcam

我正在尝试在矩形上使用VideoBrush显示我的网络摄像头。但是,我正面临着这个奇怪的问题: -

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)

然后我右键单击Silverlight应用程序,进入配置,然后进入Mic / Webcam选项卡。我在那里选择了我的网络摄像头,当我在那个对话框中时,我可以看到我的网络摄像头很好。这是我的代码: -

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();
            }
        }

    }
}

任何想法,为什么在调用CaptureSource的构造函数时执行失败?

提前致谢:)

0 个答案:

没有答案