AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

NewFrame event not called

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

NewFrame event not called

Postby warrenw » Fri Mar 23, 2018 5:02 pm

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();
            }
        }
warrenw
 
Posts: 3
Joined: Fri Mar 23, 2018 4:50 pm

Re: NewFrame event not called

Postby andrew.kirillov » Fri Mar 23, 2018 5:42 pm

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?
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: NewFrame event not called

Postby warrenw » Fri Mar 23, 2018 5:54 pm

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)
{
    ...
}
warrenw
 
Posts: 3
Joined: Fri Mar 23, 2018 4:50 pm

Re: NewFrame event not called

Postby warrenw » Fri Mar 23, 2018 6:29 pm

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.
warrenw
 
Posts: 3
Joined: Fri Mar 23, 2018 4:50 pm




Return to AForge.NET Framework

cron