Get Image from Video
Asked Answered
S

3

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

    }
Sanborn answered 22/10, 2012 at 18:22 Comment(1)
AForge.NET uses native DirectShow API to access video capture hardware. You might want to look at DirectShow.NET library instead which is the most widely used "bridge" between .NET apps and video capture APIs.Bough
S
4

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.

  1. Call Start (e.g. in the load event, or after pressing a button)
  2. Handle the frame events

    private void VideoStream_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
    {
        Bitmap newFrame = new Bitmap(eventArgs.Frame);
        pictureBox1.Image = newFrame;
    }
    
  3. 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/

Sessler answered 12/6, 2013 at 9:32 Comment(0)
M
1

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;
    }
Michaud answered 20/10, 2013 at 5:59 Comment(0)
K
0

One way to go is to get or write a wrapper around FFMPEG, which can do the work for you of extracting images from your video. I've worked on two projects that have used this means of getting thumbnails and/or stills from uploaded videos. It'll feel a little hackish, but it should work.

Kaylyn answered 22/10, 2012 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.