Page 1 of 1

NewFrame event not called

PostPosted: Fri Mar 23, 2018 5:02 pm
by warrenw
I am using the following code to display images from a webcam on a windows form, however the NewFrame event handler is never called (verified by setting a breakpoint in the handler). I do not get any exceptions when building, nor do I see and console output that would indicate an error. Can anyone point me in the right direction to figure out why this is not working?

Code: Select all
public WebCamHelper()
{
            _userPreferences = UserPreferences.Current;
            SetDevices();
}

public void SetDevices()
{
            SetDevices(
                _userPreferences.WebCamDeviceID,
                _userPreferences.WebCamResolution
            );
}

public void SetDevices(int DeviceID, string Resolution)
{
            try
            {
                videoSource = new VideoCaptureDevice(videosources[DeviceID].MonikerString);

                try
                {
                    //Check if the video device provides a list of supported resolutions
                    if (videoSource.VideoCapabilities.Length > 0)
                    {
                        for (int i = 0; i < videoSource.VideoCapabilities.Length; i++)
                        {
                            if ((videoSource.VideoCapabilities[i].FrameSize.Width + " x " + videoSource.VideoCapabilities[i].FrameSize.Height) == Resolution)
                            {
                                videoSource.VideoResolution = videoSource.VideoCapabilities[i];
                                break;
                            }
                        }
                    }
                }
                catch
                {
                    //if can't set selected resolution, choose lowest available
                    if (videoSource.VideoCapabilities.Length > 0)
                    {
                        string lowestSolution = "0;0";
                        //Search for the lowest resolution (can be changed to highest by changing [<] in line 74 to [>] and renaming variables lowestSolution -> highestSolution
                        for (int i = 0; i < videoSource.VideoCapabilities.Length; i++)
                        {
                            if (videoSource.VideoCapabilities[i].FrameSize.Width < Convert.ToInt32(lowestSolution.Split(';')[0]))
                                lowestSolution = videoSource.VideoCapabilities[i].FrameSize.Width.ToString() + ";" + i.ToString();
                        }
                        //Set the resolution as active
                        videoSource.VideoResolution = videoSource.VideoCapabilities[Convert.ToInt32(lowestSolution.Split(';')[1])];
                    }
                }

                videoSource.NewFrame += new AForge.Video.NewFrameEventHandler(videoSource_NewFrame);
            }
            catch (Exception ex)
            {
                //no-removed webcam ?
                Main cm = (Main)System.Windows.Forms.Application.OpenForms["Main"];
                cm.DisableWebCamSettingsMenu();
            }
        }

        void videoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
        {
            OnFrameReady((Bitmap)eventArgs.Frame.Clone(), null);
        }

        public void Start()
        {
            videoSource.Start();
        }

        public void Stop()
        {
            if (videoSource != null && videoSource.IsRunning)
            {
                videoSource.SignalToStop();
            }
        }

Re: NewFrame event not called

PostPosted: Fri Mar 23, 2018 5:42 pm
by andrew.kirillov
Do you get any VideoSourceError events instead? Any exceptions coming up in debugger output window?

What about adding AForge.NET Framework's sources and stepping through them?

Re: NewFrame event not called

PostPosted: Fri Mar 23, 2018 5:54 pm
by warrenw
Andrew -

I do have the sources inclued in the project, and I am not seeing any exceptions there. Thanks for the suggestion on the VideoSourceError event, unfortunately, this event is not fired either. I wasn't using it, but just added this

Code: Select all
videoSource.VideoSourceError += new VideoSourceErrorEventHandler(videoSource_Error);

...

void videoSource_Error(object sender, AForge.Video.VideoSourceErrorEventArgs eventArgs)
{
    ...
}

Re: NewFrame event not called

PostPosted: Fri Mar 23, 2018 6:29 pm
by warrenw
Found the issue!! It had NOTHING to do with the AForge libraries, so sorry for the waste of your time. There is another component in the application which was holding a reference the the camera device. Once this was cleared, the AForge events started firing as expected.