AForge.NET 是用C#寫的一個關於計算機視覺和人工智能領域的框架,它包括圖像處理、神經網絡、遺傳算法和機器學習等。
要實現視頻功能,需要使用AForge.Controls命名空間中的VideoSourcePlayer控件。這是一個WinForm控件,要在WPF程序中使用,我們需要做如下4步:
1.添加WindowsFormsIntegration應用
2.添加System.Windows.Forms.Integration命名空間
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
3.添加AForge.Controls命名空間
xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"
4.XAML中加入VideoSourcePlayer可視控件
<wfi:WindowsFormsHost Grid.Row="0" Height="320" Width="240"> <aforge:VideoSourcePlayer x:Name="videoSourcePlayer" Dock="Fill"> </aforge:VideoSourcePlayer> </wfi:WindowsFormsHost>
VideoSourcePlayer控件需要先引用
AForge.Controls.dll
AForge.Video.dll
AForge.Video.DirectShow.dll
具體代碼如下:
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(); } }