Important alert: (current site time 7/28/2014 5:59:57 AM EDT)
 

winzip icon

C# Webcam Capture

Email
Submitted on: 7/6/2003 2:40:14 PM
By: Philip Pierce  
Level: Intermediate
User Rating: By 21 Users
Compatibility: C#
Views: 335334
(About the author)
 
     This is a control I ported from a Visual Basic 6 project which will connect to a webcam and return System.Drawing.Image every x milliseconds. The control returns the image in the ImageCaptured Event. In the example, I show how to configure everything and how to display the image on the screen. It seems to run about 15 frames per second at the fastest, I think because of the overhead of converting to System.Drawing.Image

 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 21 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/21/2004 1:07:45 AMveins

This is a good one to play around.
(If this comment was disrespectful, please report it.)

 
8/6/2004 10:07:52 AM

Excellent example, exactly what i was looking for. Just one query, how would i go about implementing this in a web form?
(If this comment was disrespectful, please report it.)

 
8/18/2004 7:36:52 AM

excelent sample! it's nice to see the interaction with the webcam, without resorting to directX etc.
i have 2 questions:
1. when i ran it on my pc, it seemed to have a memory leak. have you noticed anything like this?
2. is it absolutely necessary to use the clipboard? (i thought maybe this was the leak)

regards, and thanks again for the excelent sample
(If this comment was disrespectful, please report it.)

 
10/1/2004 6:33:22 PM

can this be used with any/most web cams or do you have one that you could recommend?
(If this comment was disrespectful, please report it.)

 
12/1/2004 4:54:16 PM

Hi there, was wondering if you knew how to select different webcams with the api used? I assume that it selects thhe default one but i can't see where/how to chose a specific camera. cheers
(If this comment was disrespectful, please report it.)

 
12/23/2004 4:10:50 AMSAndrewH

I have found that using the clipboard in this fashion can eat gigs of memory after only a few minutes (depending on the quality of the image.) Does anyone have fix?
(If this comment was disrespectful, please report it.)

 
1/7/2005 12:30:01 AM

Using VS2K3 here. Nice app!

To avoid the mem usage from shooting out of site, add a call for garbage collection.

e.g., in timer1_tick(), add a line to the code getting the data from the clipboard so that it reads as:

// get from the clipboard
tempObj = Clipboard.GetDataObject();
tempImg = (System.Drawing.Bitmap) tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap);
GC.Collect();

HTH
(If this comment was disrespectful, please report it.)

 
1/25/2005 8:02:16 AM

It seems unlikely that anybody will check this but is the original VB code about anywhere? the one this was ported from? only for reasons beyond understanding i'm using VB.net and need to grab images for image analysis and motion tracking
(If this comment was disrespectful, please report it.)

 
8/9/2005 2:52:56 PMrik

In my computer
when the code run after one ~two minutes(or less),it show [An error ocurred while capturing the video image. The video capture will now be terminated.].
If i change it
[x.WebCamImage = tempImg.GetThumbnailImage(m_Width, m_Height,null, System.IntPtr.Zero);]
->
[x.WebCamImage = tempImg;]
then error will not be show
But!!
it only change to
[When the code run after one ~two minutes(or less),it will show nothing at pictureBox1]
Why it?
And how to change the code
(If this comment was disrespectful, please report it.)

 
10/3/2005 10:23:30 AMNicky

i also experience the same thing as rik.
after i run the code,it show [An error ocurred while capturing the video image. The video capture will now be terminated.]. why it happen?can someone tell me how to change the code.

(If this comment was disrespectful, please report it.)

 
2/28/2006 10:08:25 PMraja

hai, this code is working fine, but iam facing the problem out of memory, while using video camera, but its capturing image.
(If this comment was disrespectful, please report it.)

 
5/15/2006 12:07:06 AMHarshi

Excellent work! Exactly I was looking for.. I want this code to be changed to take still image in every X seconds as u have given.
Pls tell how can i improve this code to take still images
Need Help!!
(If this comment was disrespectful, please report it.)

 
5/15/2006 12:16:32 AMHarshi

This code was very useful to me. First of all i would like to thank u for publishing this code.
Code was working fine when i connected a USB webcam. i want this code to changed to take **still** images for a ggiven intervals as u did.
Pls tell me how to improve this to take still images. Need your help!!
I want this code to be changed to take still images

(If this comment was disrespectful, please report it.)

 
8/30/2006 4:07:45 AMujjwal

but i want to do it in webform and wants to see different user's webcam view.
(If this comment was disrespectful, please report it.)

 
4/4/2007 8:21:27 AMKevin

Simple and it works!! Thanks for putting this up, it will help with some development that I am doing.
(If this comment was disrespectful, please report it.)

 
5/6/2007 11:13:23 PMNupur

Excellent code....
I want to do the same in ASP.NET webform.
(If this comment was disrespectful, please report it.)

 
5/6/2007 11:19:04 PMNupur

How can we use the same code in ASP.NET webform to capture the webcam image...
(If this comment was disrespectful, please report it.)

 
5/11/2007 1:44:11 AMRaju

How can I use the same code in asp.net webform to capture the images
(If this comment was disrespectful, please report it.)

 
11/12/2008 9:54:30 AMPablo

Thanks for sharing this software!!.
(If this comment was disrespectful, please report it.)

 
12/12/2008 5:27:46 AMbimal

Excellent work! This is what exactly I was looking for. I got the same problem as Harshi. Can I know how to improve this code to take still images.

(If this comment was disrespectful, please report it.)

 
10/12/2009 3:18:29 AMne3nayka

i ve figured out the solution, taking still images!
it's important to start the wecamcapture with form control, not just some system event...;)
Now it works fine, thanks Philip!
(If this comment was disrespectful, please report it.)

 
11/18/2009 9:22:54 AMmehrdad

at thank u for your nice program i would be really appriciated if you could help me more:
i have two webcam installed on my computer and i want to capture video from both of them synchronously please please help me my job is depended on this
thanks
(If this comment was disrespectful, please report it.)

 
12/4/2009 12:09:10 PMcriollo

Excellent job!! Took all the form stuff out (nothing to click :-)) works like a charm.
(If this comment was disrespectful, please report it.)

 
1/30/2010 3:52:19 AMBendo

I am using your Webcam application in Microsoft Visual C# 2005. Great work. However I have problem to execute the .exe file (debug, release). When I push start button application stops with error message "has stopped working". It looks like SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0); does not work when i execute the program without using VS environment (when i execute the program from VS environment, everything works fine). Could you please help me? Thanks a lot.
Peter
(If this comment was disrespectful, please report it.)

 
12/15/2010 12:09:27 PMIam Muslim

thanks brother nice code

(If this comment was disrespectful, please report it.)

 
3/13/2011 11:17:11 AMerkam

Looks great!

Now I need to send the outputs (caps) via com port or USB port from the PC. First of all do you know where the outputs (caps) are stored? Second, do you know how to sink them out via com port or USB
(If this comment was disrespectful, please report it.)

 
10/5/2011 11:39:20 AMJason Scott Kelley

The links seems to be broken. I can't download the .zip file. Can anyone help?
(If this comment was disrespectful, please report it.)

 
5/3/2012 12:55:54 AMsheen

hi,
i would like to know if there is a web application version for this program.

thanks.
(If this comment was disrespectful, please report it.)

 
8/29/2012 9:59:18 AMdeepak

hey,, i am not able to use webcam component in another form please solve it...... soon
(If this comment was disrespectful, please report it.)

 
12/9/2013 6:33:02 PMManuel

Hi, I was testing the application, but I had a problem with the capture. When I tried to capture with my laptop webcam, it showed the error:

"An error ocurred while capturing the video image. The video capture will now be terminated.
Object reference not set to an instance of an object." What should I do in this case?
(If this comment was disrespectful, please report it.)

 
6/3/2014 8:41:44 AMNader Jamali

Dear Pierce
I say that your efforts on c# programming and window form application are admirable truly.
I would like to share my information on windows form with you. for beginning, I have one question for you on recording camera and saving the captured film to a file.
Your written scripts for capturing camera are very well. Nice idea.
How do I can record the film capturing online on specified path in C# or C++.
Could I have your comments or help?
Thank you.
Nader JSA
Best regards
NaderJSA

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.