同系列文章:
功能说明
- 基于AForge.NET实现视频与图像抓取
开发工具
- Visual Studio v2015
- .NET Framework 4 Client Profile
- AForge.NET 2.2.5
WPF与WinForm控件交互
要实现视频功能,需要使用AForge.Controls命名空间中的VideoSourcePlayer控件。这是一个WinForm控件,要在WPF程序中使用,我们需要做如下4步:
-
添加引用:
在.NET选项卡中选择WindowsFormsIntegration
在浏览选项卡中添加3个AForge.NET类库AForge.Controls.dll
AForge.Video.dll
AForge.Video.DirectShow.dll -
在XAML中添加System.Windows.Forms.Integration命名空间
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
-
在XAML中添加AForge.Controls命名空间
xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"
-
在XAML中加入VideoSourcePlayer可视控件
<wfi:WindowsFormsHost Grid.Row="1" Margin="8,0"> <aforge:VideoSourcePlayer x:Name="vsp"/> </wfi:WindowsFormsHost>
源代码下载
源代码
MainWindow.xaml
<Window x:Class="Splash.MainWindow" xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:Splash;assembly=FingerPictureBox" Title="FaceCapture-AForgeNET-WPF" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="480" d:DesignWidth="902" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" AllowDrop="True" Closing="Window_Closing"> <Grid AllowDrop="True"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid Grid.Column="0" AllowDrop="False"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition/> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <ComboBox Grid.Row="0" Name="comboBoxVideoDevices" FontSize="16" Margin="8,4" IsReadOnly="True"/> <wfi:WindowsFormsHost Grid.Row="1" Margin="8,0"> <aforge:VideoSourcePlayer x:Name="vsp"/> </wfi:WindowsFormsHost> <StackPanel Grid.Row="2" Orientation="Horizontal" Height="60" HorizontalAlignment="Stretch" > <Button Name="button_Play" Height="40" Width="140" Margin="40,4,20,10" Click="button_Play_Click"> <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> <Image Name="image_Play" Width="32" Height="32" /> <Label Name="label_Play" Content="开启视频" VerticalContentAlignment="Center" FontSize="16" Padding="4" /> </StackPanel> </Button> <Button Name="button_Capture" Height="40" Width="140" Margin="40,4,40,10" Click="button_Capture_Click"> <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> <Image Name="image_Capture" Width="32" Height="32" /> <Label Content="抓拍图像" VerticalContentAlignment="Center" FontSize="16" Padding="4"/> </StackPanel> </Button> </StackPanel> </Grid> <Grid Grid.Column="1"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal"> <my:FingerPictureBox Height="210" Name="fingerPictureBox1" Width="210" BorderThickness="5" BorderBrush="DarkGreen" Margin="5"/> <my:FingerPictureBox Height="210" Name="fingerPictureBox2" Width="210" BorderThickness="5" BorderBrush="DarkGreen" Margin="5"/> </StackPanel> <StackPanel Grid.Row="1" Orientation="Horizontal"> <my:FingerPictureBox Height="210" Name="fingerPictureBox3" Width="210" BorderThickness="5" BorderBrush="DarkGreen" Margin="5"/> <my:FingerPictureBox Height="210" Name="fingerPictureBox4" Width="210" BorderThickness="5" BorderBrush="DarkGreen" Margin="5"/> </StackPanel> </Grid> </Grid> </Window>
MainWindow.xaml.cs
/* ---------------------------------------------------------- 文件名称:MainWindow.xaml.cs 作者:秦建辉 微信:splashcn 博客:http://www.firstsolver.com/wordpress/ 开发环境: Visual Studio V2015 .NET Framework 4 Client Profile AForge.NET 2.2.5 版本历史: V1.0 2016年11月01日 基于AForge.NET实现视频与图像抓取 参考资料: http://www.aforgenet.com/framework/ ------------------------------------------------------------ */ using AForge.Video.DirectShow; using System; using System.Windows; using System.Windows.Media.Imaging; namespace Splash { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { BitmapSource ImagePlay; BitmapSource ImageStop; public MainWindow() { InitializeComponent(); // 设置窗体图标 this.Icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( Properties.Resources.FingerPictureBox.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); // 图像源初始化 ImagePlay = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( Properties.Resources.Button_Play_icon2.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); ImageStop = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( Properties.Resources.Button_Stop_icon.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); // 设置按钮图像 image_Play.Source = ImagePlay; image_Capture.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( Properties.Resources.capture.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); // 设置窗体装载后事件处理器 this.Loaded += new RoutedEventHandler(MainWindow_Loaded); } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { // 设定初始视频设备 comboBoxVideoDevices.ItemsSource = new FilterInfoCollection(FilterCategory.VideoInputDevice); comboBoxVideoDevices.DisplayMemberPath = "Name"; if(comboBoxVideoDevices.Items.Count > 0) { comboBoxVideoDevices.SelectedIndex = 0; if (comboBoxVideoDevices.Items.Count == 1) comboBoxVideoDevices.IsEnabled = false; } else { button_Play.IsEnabled = false; button_Capture.IsEnabled = false; } // 设置图片框初始图像 BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( Properties.Resources.noimage.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); fingerPictureBox1.InitialImage = bs; fingerPictureBox2.InitialImage = bs; fingerPictureBox3.InitialImage = bs; fingerPictureBox4.InitialImage = bs; } private void button_Play_Click(object sender, RoutedEventArgs e) { if (image_Play.Source == ImagePlay) { if (comboBoxVideoDevices.SelectedIndex != -1) { // 开启视频 vsp.VideoSource = new VideoCaptureDevice(((FilterInfo)comboBoxVideoDevices.SelectedItem).MonikerString); vsp.Start(); if (vsp.IsRunning) { // 改变按钮为“停止”状态 image_Play.Source = ImageStop; label_Play.Content = "停止"; // 允许拍照 button_Capture.IsEnabled = true; } } } else { if (vsp.IsRunning) { // 停止视频 vsp.SignalToStop(); vsp.WaitForStop(); // 改变按钮为“开始”状态 image_Play.Source = ImagePlay; label_Play.Content = "开启视频"; ; // 关闭拍照 button_Capture.IsEnabled = false; } } } private void button_Capture_Click(object sender, RoutedEventArgs e) { // 判断视频设备是否开启 if (vsp.IsRunning) { // 进行拍照 for (Int32 i = 1; i <= 4; i++) { object box = this.FindName("fingerPictureBox" + i); if (box is FingerPictureBox) { if ((box as FingerPictureBox).ActiveImage == (box as FingerPictureBox).InitialImage) { // 更新图像 (box as FingerPictureBox).ActiveImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( vsp.GetCurrentVideoFrame().GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); break; } } } } } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (vsp.IsRunning) { // 停止视频 vsp.SignalToStop(); vsp.WaitForStop(); } } } }