在WPF中使用AForge控件

AForge.NET 是用C#写的一个关于计算机视觉和人工智能领域的框架,它包括图像处理、神经网络、遗传算法和机器学习等。

要实现视频功能,需要使用AForge.Controls命名空间中的VideoSourcePlayer控件。这是一个WinForm控件,要在WPF程序中使用,我们需要做如下4步:

1.添加WindowsFormsIntegration应用

在WPF中使用AForge控件

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();
            }
        }

 

在WPF中使用AForge控件

上一篇:c# – 扩展工具包SplitButton:使用ComboBox ItemsSource作为DropDownContent


下一篇:实验13