Get Image from Video
Solution 1
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.
- Call Start (e.g. in the load event, or after pressing a button)
-
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/
Solution 2
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;
}
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on June 04, 2022Comments
-
Admin about 2 years
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(); }