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();
}
Extract Frames from Video using FFMpeg. For experienced users, FFMpeg is also a good way to extract frames from video with high quality. FFMpeg is a very popular command line-based tool to process, convert and manipulate video, audio, and other multimedia files.
This tool allows you to convert video to an image sequence. Upload any type of video and it will generate a list of JPG (JPEG) images. Upload your video, select size and frames per second, choose the part of the video you want to convert, and click "Convert to JPG!"
If I remember correctly the bitmap needs to be copied right away since it is overwritten after the event. Using a reference is no good here. Try something like:
private void video_NewFrame( object sender, NewFrameEventArgs eventArgs )
{
// copy the new frame
bitmap = new Bitmap(eventArgs.Frame);
// process the frame
}
or
private void video_NewFrame( object sender, NewFrameEventArgs eventArgs )
{
// clone new frame
bitmap = eventArgs.Frame.Clone();
// process the frame
}
Also you should not use an extra thread for this, AForge already does that.
Handle the frame events
private void VideoStream_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
Bitmap newFrame = new Bitmap(eventArgs.Frame);
pictureBox1.Image = newFrame;
}
Call Stop (closing event or button)
If you run into issues with WinForm controls e.g. a label you need to know that these controls were created on another thread and you need to use Invoke. E.g:
label_ms.Invoke((MethodInvoker)(() => label_ms.Text = msTimeSpan.TotalMilliseconds.ToString()));
Your best bet is to check out this AForge sample that comes with the framework: http://aforge.googlecode.com/svn/trunk/Samples/Video/Player/
I am using the Aforge video library with my Foscams, and it is working very well. Here is my code for the event handler.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With