我需要什么控件来显示网络摄像头的视频?
我想集成一个网络摄像头,并且我正在使用 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();
}
}
}
I want to integrate a webcam and I am using Aforge.NET framework. What control do I need to show the webcam video. My simple program will get the webcam, put it in the combo box and when i click 'ok', I can see that the camera is running but I don't see the video. 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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WPF 中没有为此构建任何内容。但我可以推荐 Jeremiah Morrill 的 WPF MediaKit,您可以在 http://wpfmediakit.codeplex.com/ 找到它。
该工具包中的
VideoCaptureElement
可以将来自任何捕获源(包括任何网络摄像头)的视频渲染到 WPF 中。There's nothing built into WPF for this. But I can recommend Jeremiah Morrill's WPF MediaKit, which you can find at http://wpfmediakit.codeplex.com/
The
VideoCaptureElement
in that toolkit can render video from any capture source (which would include any web cam) into WPF.我们正在使用 VLC Dot NET Control。这就像一个魅力,也适用于网络摄像头: http://vlcdotnet.codeplex.com/
We are using VLC Dot NET Control. This is working like a charm, also with Webcam: http://vlcdotnet.codeplex.com/