我想集成一个网络摄像头,我正在使用 Aforge.NET 框架。我需要什么控件来显示网络摄像头视频。我的简单程序将获取网络摄像头,将其放入组合框中,当我单击“确定”时,我可以看到摄像头正在运行,但我看不到视频。C#/WPF
`
<Window x:Class="MyWebCam.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="425">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.ColumnSpan="2" x:Name="videoPlayer" Margin="15"></Image>
<ComboBox Grid.Row="1" Grid.ColumnSpan="2" x:Name="cbxDevices" MinWidth="150" Margin="15" VerticalAlignment="Center" HorizontalAlignment="left" SelectionChanged="cbxDevices_SelectionChanged" />
<Button Grid.Column="1" Grid.Row="1" x:Name="btnOk" Content="OK" MinWidth="100" Margin="15" Click="btnOk_Click" />
</Grid>
</Window>
`
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AForge;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.VFW;
using AForge.Video.DirectShow;
using AForge.Controls;
using System.Drawing;
using AForge.Vision.Motion;
using AForge.Vision;
namespace MyWebCam
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//opened video source
private VideoCaptureDevice videoSource = null;
//motion detector
MotionDetector detector = new MotionDetector(
new TwoFramesDifferenceDetector(),
new MotionAreaHighlighting());
FilterInfoCollection videoDevices;
private bool deviceExist = false;
private string device;
//video device
public string VideoDevice
{
get { return device; }
}
public MainWindow()
{
InitializeComponent();
getCamList();
}
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
cbxDevices.Items.Clear();
if (videoDevices.Count == 0)
{
throw new ApplicationException();
}
deviceExist = true;
foreach (FilterInfo device in videoDevices)
{
cbxDevices.Items.Add(device.Name);
}
cbxDevices.SelectedIndex = 0; //make first cam default
}
catch (ApplicationException)
{
deviceExist = false;
cbxDevices.Items.Add("No webcams on your system");
}
}
private void cbxDevices_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
device = videoDevices[cbxDevices.SelectedIndex].MonikerString;
if (cbxDevices.SelectedIndex != -1)
{
videoSource = new VideoCaptureDevice(videoDevices[cbxDevices.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
}
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
// get new frame
Bitmap video = (Bitmap)eventArgs.Frame;
// process the frame
videoPlayer.Source = video; //this is where i'm having the problem
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
videoSource.Start();
}
}
}