AForge.NET is a framework about computer vision and artificial intelligence written by C, which includes image processing, neural network, genetic algorithm and machine learning.
To implement video functionality, you need to use the VideoSourcePlayer control in the AForge.Controls namespace. This is a WinForm control. To use it in WPF program, we need to do the following four steps:
1. Add windows forms integration application
2. Add System.Windows.Forms.Integration namespace
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
3. Add AForge.Controls namespace
xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"
4. Add VideoSourcePlayer visual control to Xaml
<wfi:WindowsFormsHost Grid.Row="0" Height="320" Width="240"> <aforge:VideoSourcePlayer x:Name="videoSourcePlayer" Dock="Fill"> </aforge:VideoSourcePlayer> </wfi:WindowsFormsHost>
VideoSourcePlayer control needs to be referenced first
AForge.Controls.dll
AForge.Video.dll
AForge.Video.DirectShow.dll
The specific code is as follows:
public MainWindow() { InitializeComponent(); videoSourcePlayer.NewFrame += VideoSourcePlayer_NewFrame; videoSourcePlayer.Height = 320; videoSourcePlayer.Width = 240; } private void VideoSourcePlayer_NewFrame(object sender, ref System.Drawing.Bitmap image) { } private void Window_Loaded(object sender, RoutedEventArgs e) { MJPEGStream mjpegSource = new MJPEGStream("http://192.168.191.1:8080"); OpenVideoSource(mjpegSource); } private void OpenVideoSource(IVideoSource source) { videoSourcePlayer.SignalToStop(); videoSourcePlayer.WaitForStop(); videoSourcePlayer.VideoSource = source; videoSourcePlayer.Start(); } private void Window_Unloaded(object sender, RoutedEventArgs e) { if (videoSourcePlayer.VideoSource != null) { videoSourcePlayer.SignalToStop(); videoSourcePlayer.WaitForStop(); } }