wpf - 使用 AForge.Net 在 WPF 应用程序上实现网络摄像头

标签 wpf webcam aforge

我正在编写一个 WPF 应用程序,我需要在其中显示网络摄像头提要。我可以使用 AForge 框架轻松完成此操作。但是当我从一台计算机更换到另一台计算机时,相同的代码无法以相同的方式工作。

在第一个中,网络摄像头馈送工作完美,但在另一个中没有发生,馈送有很多延迟,并且应用程序无法正常工作。

这是代码:

    private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap img = (Bitmap)eventArgs.Frame.Clone();

        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, (SendOrPostCallback)delegate
            {
                IntPtr hBitmap = img.GetHbitmap();
                System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                DeleteObject(hBitmap);

                img.Dispose();
                GC.Collect();
                image1.Source = bitmapSource;

            }, null);

    }

这段代码很简单,它从网络摄像头中以Bitmap 的形式获取了一个new_frame。 ,而我需要做的是将其转换为 BitmapSource ,所以我可以在 WPF 的图像框中显示。我认为这种转换是造成困惑的原因,但我不明白为什么它在计算机中有效,而在其他计算机中却没有。

计算机规范几乎相同,处理器相同,系统内存也相同。

我的问题是关于 性能 ,此代码在一台计算机上运行流畅,并且网络摄像头馈送按其应有的方式呈现,当我将其移植到另一台 PC 时,这不会发生。

最佳答案

这是基于 this 的工作代码文章。

(1) Download and install last AForge framework . (我用的是 2.2.4 版本)

(2) 创建 WPF 应用程序项目。

(3) 添加对那些 AForge DLL 的引用。 (您可以在 C:\Program Files (x86)\AForge.NET\Framework\Release 文件夹下找到它们,即)

enter image description here

(4) 构建您的项目。 (我用过 VS 2012)

(5) 添加WPF Image控件,命名为“frameHolder”。

所以你有类似的东西

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Image HorizontalAlignment="Stretch" Name="frameHolder"  VerticalAlignment="Stretch"  Stretch="Fill"/>
    </Grid>
</Window>

(6) 添加C#代码:
using AForge.Video;
    using AForge.Video.DirectShow;
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    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;

/////
namespace WpfApplication1
    {
        public partial class MainWindow : Window
        {
            VideoCaptureDevice LocalWebCam;
            public FilterInfoCollection LoaclWebCamsCollection; 

        void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            try
            {
                System.Drawing.Image img = (Bitmap)eventArgs.Frame.Clone();

                MemoryStream ms = new MemoryStream();
                img.Save(ms, ImageFormat.Bmp);
                ms.Seek(0, SeekOrigin.Begin);
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = ms;
                bi.EndInit();

                bi.Freeze();
                Dispatcher.BeginInvoke(new ThreadStart(delegate
                {
                    frameHolder.Source = bi;
                }));
            }
            catch (Exception ex)
            {
            }
        } 

        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            LoaclWebCamsCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            LocalWebCam = new VideoCaptureDevice(LoaclWebCamsCollection[0].MonikerString);
            LocalWebCam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);

            LocalWebCam.Start();
        }
    }
}

(7) 重新构建项目,它的工作原理!

注:我们默认使用第一个检测到的网络摄像头。
确保您安装了网络摄像头驱动程序并且网络摄像头正常工作...... :)

关于wpf - 使用 AForge.Net 在 WPF 应用程序上实现网络摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2006055/

相关文章:

WPF 应用程序消息循环和 PostThreadMessage

wpf - 来自源的背景图像数据绑定(bind)不起作用

wpf - ListItem 模板 - 内容未显示

opencv - 在网络摄像头提要opencv中检测两条线之间的交点

python - 无法在 Ubuntu 16.04(主机)上使用 OpenCV 中的集成网络摄像头

c# - 自动对焦例程检测非常小的模糊差异

c# - 使用 AForge.Video.FFMPEG/AForge.Video.VFW 截屏视频。帧率问题

wpf - 覆盖覆盖的 WPF 主题

java - windows7上的java编程问题(在windows xp上运行良好)

image-processing - 在 AForge.NET 中使用霍夫变换检测图像中的同心圆