Click here to Skip to main content
15,889,795 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi,
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:
C#
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:
C#
private void VideoRecording()
{
    camImg.videoSource.Start();

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

}


Thank You for Your replies!

-Pepin z Hané
Posted
Updated 23-Jan-19 7:42am
Comments
Sushil Mate 19-Oct-12 0:20am    
first of all are you able to capture the video? if yes then event is getting called?
put the debug pointer & check the value of bitmap..
Pepin z Hane 19-Oct-12 3:03am    
I am trying to capture the video and I don't know how. I only can recognize camera devices, so the user can choose the camera from a combo box. And I don't know if the event is called. I don't think so, because the bitmap is null during the debuging as I have written.
Sushil Mate 19-Oct-12 3:07am    
you need to first grab the video then go for the image frame.. divide the work & try to solve it :)
Pepin z Hane 20-Oct-12 17:08pm    
If I had any idea how to do it... :-)
Sushil Mate 21-Oct-12 7:50am    
OriginalGriff has the idea, go with it :)

From your brief discussion with Sushil Mate, it appears your problem is pretty basic: you need to break the task up into "chunks" and get the first one working before you move on to the next, just as he says.

Start by getting video capture working! Until you have that, you have nothing to get frames from! :laugh:

This may help: C# \ VB.NET Camera Communication Libraries[^] (you could have found this yourself with a quick Google - it took me only seconds and was the third hit on a simple search: "get video from camera c#")
 
Share this answer
 
I have solved it. In Constructor I have initialized an event, but in Form1 I was overwriting videoSource, so the event wasn't initialized.

The Right code:
C#
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
        private VideoCaptureDevice _videoSource;
        public Bitmap bitmap = new Bitmap(10, 10);
        public VideoCaptureDevice videoSource
        {
            get
            {
                return _videoSource;
            }
            set
            {
                _videoSource = value;
                // set NewFrame event handler
                _videoSource.NewFrame +=new NewFrameEventHandler(videoSource_NewFrame);
            }
        }
        //screen shot
        
        public CameraImaging(VideoCaptureDevice videoDevice)
        {
            // create video source
            VideoCaptureDevice videoSource = videoDevice;
        }
        public CameraImaging()
        {

        }
        private void videoSource_NewFrame( object sender, NewFrameEventArgs eventArgs )
        {
            // get new frame
            lock (bitmap)
            {
                bitmap = (Bitmap)eventArgs.Frame.Clone();
                // process the frame
            }
            
        }
    }
}

Form1:
C#
...
private void VideoRecording()
        {
            camImg.videoSource.Start();
            
            while (!StopVideo)
            {
                lock (mImageLock)
                {
                    Bitmap tmp = (Bitmap)camImg.bitmap.Clone();

                    if (InvokeRequired)
                    {
                        BeginInvoke(new MethodInvoker(() =>
                            {
                                pictureBox1.Image = tmp;
                                pictureBox1.Invalidate();
                            }));
                    }
                    else
                    {
                        pictureBox1.Image = tmp;
                        pictureBox1.Invalidate();
                    }
                }
                Thread.Sleep(33);
            }
            camImg.videoSource.Stop();
            
        }
        private void btnStartVideo_Click(object sender, EventArgs e)
        {
            StopVideo = false;
            try
            {
                camImg.videoSource = new VideoCaptureDevice (camImg.videoDevices[cbCameraDevices.SelectedIndex].MonikerString);
                thrVideo = new Thread(VideoRecording);
                lblRecording.Visible = true;
                thrVideo.Start();
                Thread.Sleep(1000);
            }
            catch (Exception)
            {
                MessageBox.Show("No camera was chosen!", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            
            
        }
...
 
Share this answer
 
Comments
Member 9829197 24-Jan-14 2:06am    
I have not camera conneted.
how to get all frames from already captured video?
which formats are supported by "AForge" ?
can you please provide me some code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900