Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

WebCam Application with Scheduling

, 8 Mar 2011
Rate this:
Please Sign up or sign in to vote.
WebCam Application with Scheduling

Introduction

This application is about using a webCam to capture video to a file (.avi). It also has an ability to schedule recording times to start the video capturing automatically. We can use any WebCam and set all kinds of properties to use with the WebCam like video size and source and the WebCam settings itself.

Background

The base for this code is using the avicap32 DLL. With this DLL, you can connect to the webCam and set its settings.
We use SendMessage to send messages to the WebCam like Connect and capture video. There are a few articles about this issue, but I will try to make this a very simple one.

Using the Code

The first thing we need to do is to set our capturing control, I am using a simple PictureBox Control to host the output of the WebCam.
This is done with only one line of code:
mCapHwnd = capCreateCaptureWindowA("WebCap", WS_VISIBLE | WS_CHILD, 0, 0,
this.pictureBox1.Width, this.pictureBox1.Height, this.pictureBox1.Handle.ToInt32(), 0);
The next thing we need to do is to connect to our WebCam:
SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0);
Next we need some lines of code to set the webcam settings:
//Set the frame rate for the WebCam
SendMessage(mCapHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);
//Set the preview flag to true
SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 1, 0);
//Set thisif you want to control the video compression for the output video
SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0);
For capturing video, you must set all kinds of video properties like if you want to capture audio, etc. We need to use the CAPTUREPARMS struct for that, you can find its declaration inside the source code download.
The next line is using the CAPTUREPARMS struct to set the WebCam setting we want:
SendMessage2(mCapHwnd, WM_CAP_SET_SEQUENCE_SETUP,
 new IntPtr(Marshal.SizeOf(CaptureParams)),ref CaptureParams);
One importent param in the CAPTUREPARMS struct is fYield , set this to true (1) if you want to capture the video in a different thread than your form.
The next thing we want to do is to capture the video in to the PictureBox control. We do this by calling this message:
SendMessage(mCapHwnd, WM_CAP_GRAB_FRAME_NOSTOP, 0, 0);
We need to call this line inside a timer tick event so we get a continuous video I have used a System.Threading.Timer to do this.
private void DoIt()
{
System.Threading.Timer t = null;
t = new System.Threading.Timer(delegate(object state)
{
t.Dispose();
CaptureImage();
DoIt();
}, null, 66, -1);
}
The next thing we need to do is to capture the video to a file (.avi file is the default file using the avicap32.dll)
string file = DateTime.Now.ToShortDateString().Replace('/','_') +
 "_" + DateTime.Now.ToLongTimeString().Replace(':','_') + ".avi";
SendMessageA(mCapHwnd, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, dir +
 Path.DirectorySeparatorChar + file);
SendMessage(mCapHwnd, WM_CAP_SEQUENCE, 0, 0);
The next two methods are used to config the WebCam Source and Format. Use this only if you dont want to use the defaults of your WebCam.
private void btnConfig_Click(object sender, EventArgs e)
{
SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOSOURCE, 0, 0);
}
private void btnFormat_Click(object sender, EventArgs e)
{
SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0);
}

The scheduling part of the code is very simple. I am using the app.config file to store the time I want to start capturing and use a Timer control to check if it is time to start capturing. You can check this out in the source code download.

Ok, the only thing left to do it to disconnect from the WebCam when the work is done:
SendMessage(mCapHwnd, WM_CAP_DISCONNECT, 0, 0);
That's it. Hope I succeeded in keeping it simple.

Points of Interest

This is my first application using a WebCam. You can do the same with directX, but I found this method to be the fast and simple.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ron Levy
Software Developer
Israel Israel
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 1 Pinmemberhaji124519-Jan-13 22:33 
QuestionStream Audio PinmemberDaMonkeyFunk9-Jan-13 10:56 
Questionthis webcam in laptop PinmemberAtif BOUZAGLAOUI8-Jun-12 10:57 
Questionhow can i make this webcam with other webcam PinmemberAtif BOUZAGLAOUI6-Jun-12 13:57 
QuestionWebCam Application with Scheduling Pinmemberdongcr25-Mar-12 23:30 
GeneralReason for my vote of 5 Esto me ahorro mucho tiempo. Pinmemberedgartaor18-Feb-12 10:45 
QuestionCapture picture PinmemberNoam Parvin23-Aug-11 8:52 
Generalpicture box Pinmemberpater4297-Mar-11 12:02 
GeneralRe: picture box Pinmemberpater4298-Mar-11 2:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 8 Mar 2011
Article Copyright 2011 by Ron Levy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid