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