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

C# Silverlight网络摄像头API抛出奇怪错误,c#,silverlight,silverlight-4.0,webcam,C#,Silverlight,Silverlight 4.0,Webcam,我正试图用视频画笔在矩形上显示我的网络摄像头。然而,我面临着一个奇怪的问题:- External component has thrown an exception. at MS.Internal.XcpImports.CaptureGraph_GetAvailableVideoCaptureDevicesNative(IntPtr pContext, Int32& typeIndex, CValue& DeviceCollection) at MS.Interna

我正试图用视频画笔在矩形上显示我的网络摄像头。然而,我面临着一个奇怪的问题:-

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应用程序,进入配置,然后进入麦克风/网络摄像头选项卡。我在那里选择了我的网络摄像头,当我在对话框中时,我可以看到我的网络摄像头很好。这是我的密码:-

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

    }
}
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Media;
使用制度;
命名空间FMT.Client.Views.ChildWindows
{
公共部分类网络摄像头:ChildWindow
{
私人CaptureSource(来源);
私人捕获设备(网络摄像头),;
私人视频刷(webcamBrush),;
私人图像刷_capturedImage;
公共网络摄像机()
{
初始化组件();
this.Loaded+=网络摄像头\u Loaded;
}
已加载无效网络摄像头(对象发送器,路由目标e)
{
尝试
{

//如果(CaptureDeviceConfiguration.GetAvailableDeviceCaptureDevices().Count)返回(CaptureDeviceConfiguration.GetAvailableDeviceCaptureDevices().Count
返回),请在调试器之外尝试(某些Cam驱动程序确实不喜欢调试器)@Jaroslav Jandek:-是的,现在它可以工作了。需要做的另一个更改是,仅在单击按钮时调用RequestAccess函数。即使子窗口本身在单击按钮时打开。但是RequestAccess必须从其他按钮进一步调用CaptureDeviceConfiguration.RequestDeviceAccess()将以静默方式失败。它在加载的事件中不起作用。这就是为什么到目前为止我看到的所有示例都有一个格式为:-)的开始按钮。这可能是微软另一个愚蠢的限制。@Kar Cheng:这项功能实际上是有意义的。我不希望SL应用程序以静默方式使用我的cam。如果它在任何地方都被允许,而你可以自动弹出请求,它仍然可以用来向用户发送垃圾邮件…@Jaroslav Jandek:-如何??CaptureDeviceConfiguration.RequestDeviceAccess()用于获取用户权限。没有用户权限,SL不能静默使用cam。只有当用户明确单击“确定”按钮时,SL才能使用。此限制意味着即使是“获取用户使用网络摄像头的权限,我们也需要由用户启动操作”这在我看来是不合适的。@Kar Cheng:请阅读全部评论,我已经说明了原因。让我重申一下:在没有用户交互的情况下,程序可以无声地使用cam或(在当前的实现中)通过确认窗口向用户发送垃圾邮件。这是我无法接受的。