10

I am trying to write an application which can access cameras connected to PC, record a video and get an image from the video. I use AForge.NET libraries to access cameras: http://www.aforgenet.com/framework/

I don't know how the event named AForge.Video.NewFrameEventHandler works. In this code the event returns null to a bitmap instead of a new frame from a video or the event is not called. I want to get frames from the video to a picture box every time frame to make something like a video stream and after click on the stop button I want the last image to stay displayed in the picture box. Does anyone know how? And why my code doesn't work?

Code:

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
        }
    }
}

The similar code is here: http://www.aforgenet.com/framework/features/directshow_video.html[^]

In the Windows Forms I run this video in a thread which does this method:

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

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

    }
4

3 回答 3

4

如果我没记错的话,需要立即复制位图,因为它在事件发生后被覆盖。在这里使用参考并不好。尝试类似:

private void video_NewFrame( object sender, NewFrameEventArgs eventArgs )
{
    // copy the new frame
    bitmap = new Bitmap(eventArgs.Frame);
    // process the frame
}

或者

private void video_NewFrame( object sender, NewFrameEventArgs eventArgs )
{
   // clone new frame
   bitmap = eventArgs.Frame.Clone();
   // process the frame
}

此外,您不应该为此使用额外的线程,AForge 已经这样做了。

  1. 调用开始(例如在加载事件中,或按下按钮后)
  2. 处理帧事件

    private void VideoStream_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
    {
        Bitmap newFrame = new Bitmap(eventArgs.Frame);
        pictureBox1.Image = newFrame;
    }
    
  3. 呼叫停止(关闭事件或按钮)

如果您遇到 WinForm 控件的问题,例如标签,您需要知道这些控件是在另一个线程上创建的,并且您需要使用 Invoke。例如:

label_ms.Invoke((MethodInvoker)(() => label_ms.Text = msTimeSpan.TotalMilliseconds.ToString()));

最好的办法是查看框架附带的 AForge 示例:http: //aforge.googlecode.com/svn/trunk/Samples/Video/Player/

于 2013-06-12T09:32:35.847 回答
1

我在我的 Foscams 中使用 Aforge 视频库,它运行良好。这是我的事件处理程序代码。

private void Video_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
    {
        //Create Bitmap from frame
        Bitmap FrameData = new Bitmap(eventArgs.Frame);
        //Add to PictureBox
        PictureBox.Image = FrameData;
    }
于 2013-10-20T05:59:47.560 回答
0

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

于 2012-10-22T18:27:59.160 回答