Page 1 of 1

Capture from Webcam not working

PostPosted: Tue Mar 03, 2009 4:19 am
by aish0
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();
        }
...

Re: Capture from Webcam not working

PostPosted: Tue Mar 03, 2009 6:59 am
by andrew.kirillov
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.

Re: Capture from Webcam not working

PostPosted: Tue Mar 03, 2009 5:28 pm
by aish0
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

Re: Capture from Webcam not working

PostPosted: Tue Mar 03, 2009 7:54 pm
by andrew.kirillov
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.