AForge.NET

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

Capture from Webcam not working

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

Capture from Webcam not working

Postby aish0 » Tue Mar 03, 2009 4:19 am

Hey,

I tried the following code. When I run the project, I see a blank form, that is all (instead of the camera output). Setting the message boxes in each location, i expected the message box to keep popping for every new frame. This never happened!

I hope you can help me debug this:

Code: Select all
Class Form1
...
        private VideoCaptureDevice webcam;

        public Form1()
        {
            MessageBox.Show("0");
            InitializeComponent();
            MessageBox.Show("1");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FilterInfoCollection f = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            webcam = new VideoCaptureDevice(f[3].MonikerString);
            webcam.NewFrame += new NewFrameEventHandler(webcam_NewFrame);
            webcam.Start();
            webcam.SignalToStop();
        }

        private void webcam_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap newFrame = eventArgs.Frame;
            pictureBox1.Image = newFrame;
            MessageBox.Show("_");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            webcam.Stop();
        }
...
aish0
 
Posts: 16
Joined: Tue Mar 03, 2009 4:11 am

Re: Capture from Webcam not working

Postby andrew.kirillov » Tue Mar 03, 2009 6:59 am

Hello,

Personally I would not expect anything from the provided code. Of course it will give nothing, because you signal camera to stop right after starting it.

What is this for?
aish0 wrote: webcam.Start();
webcam.SignalToStop();


See documentation to SignalToStop() - it is stated there that camera will not provide any frame after this.
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: Capture from Webcam not working

Postby aish0 » Tue Mar 03, 2009 5:28 pm

Hey, Yes, that was my mistake. I realized it. Removing the SignalToStop and running it gave me an unhandled exception.

To get around that, I had to add "System.Threading.Thread.Sleep(40)" in the newFrame handler...I guess otherwise, too much was trying to be forced into the pictureBox in too little time

Using Thread.Sleep(40), is that a good approach or will there be any side-effects that you can think of?

Also, now when I resize the form, I almost always get an exception. I am guessing, this has to do with the pictureBox size and the frame size (from the newFrame handler) going out of synch...is this correct? (I have the pictureBox's dock property set to Fill)

Thanks,
Aish
aish0
 
Posts: 16
Joined: Tue Mar 03, 2009 4:11 am

Re: Capture from Webcam not working

Postby andrew.kirillov » Tue Mar 03, 2009 7:54 pm

Hello,

aish0 wrote:Removing the SignalToStop and running it gave me an unhandled exception.

Well, the same as before - you just did not read docs carefully. See documentation to NewFrame event. So, to solve your issue, you just need to clone image before giving it to picture box.
Code: Select all
pictureBox.Image = (Bitmap) newFrame.Clone( );


If you do so, you will not need to use Sleep() and you will get rid of exception on resizing.
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




Return to AForge.NET Framework

cron