从视频中获取图像

Pep*_*ané 10 .net c# video-processing aforge

我正在尝试编写一个应用程序,它可以访问连接到PC的摄像机,录制视频并从视频中获取图像.我使用AForge.NET库来访问摄像头:http://www.aforgenet.com/framework/

我不知道名为AForge.Video.NewFrameEventHandler的事件是如何工作的.在此代码中,事件将null返回到位图而不是视频中的新帧,或者不调用该事件.我希望每次都能从视频中获取帧到一个图片框,以制作类似于视频流的内容,点击停止按钮后,我希望最后一个图像保持显示在图片框中.有谁知道怎么样?为什么我的代码不起作用?

码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AForge.Video.DirectShow;
using System.Drawing;
using AForge.Video;

namespace CameraDevice
{
    public class CameraImaging
    {
        // enumerate video devices
        public FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice );
        //camera
        public VideoCaptureDevice videoSource;
        //screen shot
        public Bitmap bitmap;
        public CameraImaging()
        {
            // create video source
            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString );
            // set NewFrame event handler
            videoSource.NewFrame += new NewFrameEventHandler( video_NewFrame );
        }
        public void StartVideo(VideoCaptureDevice videoSource)
        {
            // start the video source
            videoSource.Start();
            // ...
        }
        public void StopVideo(VideoCaptureDevice videoSource)
        {
            // stop the video source
            videoSource.Stop();
            // ...
        }
        private void video_NewFrame( object sender, NewFrameEventArgs eventArgs )
        {
            // get new frame
            bitmap = eventArgs.Frame;
            // process the frame
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

类似的代码在这里:http://www.aforgenet.com/framework/features/directshow_video.html [ ^]

在Windows窗体中,我在一个执行此方法的线程中运行此视频:

private void VideoRecording()
    {
        camImg.videoSource.Start();

        while (!StopVideo)
        {
            pictureBox1.Image = camImg.bitmap;
            pictureBox1.Invalidate();
        }
        camImg.videoSource.Stop();

    }
Run Code Online (Sandbox Code Playgroud)

Ben*_*esh 0

一种方法是获取或编写 FFMPEG 的包装器,它可以为您完成从视频中提取图像的工作。我参与过两个项目,它们使用这种方法从上传的视频中获取缩略图和/或静态图片。它会感觉有点hackish,但它应该可以工作。