Click here to Skip to main content
15,920,102 members
Articles / Desktop Programming / Windows Forms

Webcam application with scheduling

Rate me:
Please Sign up or sign in to vote.
4.25/5 (7 votes)
4 Dec 2011CPOL2 min read 39.2K   6.9K   37   7
A webcam application to capture video with scheduling ability.


This application is about using a webcam to capture video to a file (.avi). It also has the 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 themselves.


The base for this code is using the avicap32 DLL. With this DLL, you can connect to a webcam and set its settings. We use SendMessage to send messages to the webcam like Connect and Capture Video. There are a few existing articles about this topic 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:

 new IntPtr(Marshal.SizeOf(CaptureParams)),ref CaptureParams);

One important param in the CAPTUREPARMS struct is fYield. Set this to true (1) if you want to capture the video in a different thread then your form.

The next thing we want to do is capture the video into 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 continues 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)
    }, null, 66, -1);

The next thing we need to do is capture the video to a file (a .avi file is the default file using 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 don't 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 using 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 is 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 fast and simple.


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

Written By
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Questionweb cam Pin
Nizam Ul Haq2-Dec-14 2:48
Nizam Ul Haq2-Dec-14 2:48 
Questionandriod games Pin
Nizam Ul Haq2-Dec-14 2:40
Nizam Ul Haq2-Dec-14 2:40 
GeneralMy vote of 1 Pin
programmerdon4-Dec-12 3:13
programmerdon4-Dec-12 3:13 
QuestionHow to setting the video‘s format with out dialog? Pin
gwf_25sz2-Dec-12 20:13
gwf_25sz2-Dec-12 20:13 
BugThis application does't work on the second time. Pin
Cao Huan2-Feb-12 15:40
Cao Huan2-Feb-12 15:40 
GeneralRe: This application does't work on the second time. Pin
miguelsanc15-Aug-12 10:33
miguelsanc15-Aug-12 10:33 
GeneralRe: This application does't work on the second time. Pin
Cao Huan15-Aug-12 14:56
Cao Huan15-Aug-12 14:56 
WPF: Webcam Control[^

I wish this link will help u.

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

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