使用 AForge.Net 在 WPF 应用程序上实现网络摄像头 [英] Implementing a WebCam on a WPF App using AForge.Net

查看:73
本文介绍了使用 AForge.Net 在 WPF 应用程序上实现网络摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing an WPF application where I need to show a Webcam feed. I was able to do this easly with the AForge framework.But when I've changed from a computer to a another computer the same code doesn't work the same way.

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

In the first one the webcam feed works perfectly, but in the other one this does't occur, the feed has a lot of delay, and the application doesn't work properly.

代码如下:

    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 的图像框中显示.我认为这种转换是造成混乱的原因,但我不明白为什么它在计算机中有效,而在其他计算机中却没有.

What this code is really simple, it gets a new_frame from the webcam in a form of a Bitmap, and what I need to do is to convert it to a BitmapSource, so I can show in the image frame of the WPF. I think this conversion is the responsible of the mess that is happening, but I don't understand why it works in a computer and in the other doesn't.

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

The computer specs are almost the same, the processor is the same, as well the system memory.

我的问题是关于性能,此代码在一台计算机上运行流畅,并且网络摄像头提要按原样显示,当我将其移植到另一台 PC 时,这不会发生.

My problem here is about performance, this code in one computer runs smoothly, and the webcam feed is presented as it should, when I port it to another PC this doesn't happen.

推荐答案

这是基于 这篇 文章.

(1) 下载并安装最新的 AForge 框架.(我用的是2.2.4版本)

(1) Download and install last AForge framework. (I have used version 2.2.4)

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

(2) Create WPF Application project.

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

(3) Add references to those AForge DLLs. (You can find them under C:\Program Files (x86)\AForge.NET\Framework\Release folder i.e.)

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

(4) Build your project. (I have used VS 2012)

(5) 添加WPF Image控件并命名为frameHolder".

(5) Add WPF Image control and name it "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# 代码:

(6) Add C# code:

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) 重新构建项目,它可以工作了!

(7) Re-Build project and it works!

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

Note: We use first detected WebCam by default. Make sure you have WebCam driver insalled and WebCam is working in general... :)

这篇关于使用 AForge.Net 在 WPF 应用程序上实现网络摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆