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
{
_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:)
source
share