Controlling Your Web Camera Using C#

I have recently been looking for a C# project to control my web cam or an attached video camera.  Apparently, this is a tall order since most video camera controllers are written in very low level C or C++ and not easily wrapped in C# without using P/Invoke and many Dllimport statements (i.e. un-managed and possibly unsafe code).

I did stumble across the Versatile C# Webcam Library on Code Project.  However, after downloading the project’s source code, it took me quite a while to get it to compile in Visual Studio 2010.  Apparently, one of the C++ libraries used (qedit.h)  is no longer included with Windows 7.  After reading countless comments, adding a custom header file for qedit.h, and tinkering, I was able to get this project to compile in Visual Studio 2010.

For anyone who is interested, you can find the working code below!

EDIT:

A copy of the source code compiled in release mode can be found here HERE (version 2).

A “Published” version of the code can be found HERE.

Additional Resources:

Here is a link to the DirectShow.Net project on Sourceforge.  If you still have found what you are looking for, I believe this DirectShow.dll contains the most comprehensive set of features for controlling video.  I have not really tested this .NET wrapper, but it looks like it covers just about everything in DirectShow, and it comes with tons of example projects!

Here is a link to a very simple .NET wrapper for Microsoft’s latest video controller WIA.

Here are a few Video links from StackOverflow as well:

http://stackoverflow.com/questions/4953211/how-do-i-capture-video-from-a-webcam

http://stackoverflow.com/questions/233455/webcam-usage-in-c-sharp

60 thoughts on “Controlling Your Web Camera Using C#

  1. Appears to be just what I was after. A real newby to C#, looking for a little project to use an unwanted webcam and a proven set of code that can be explored to develop knowledge and skills.

    Just a little question-from newby knowledge level-are all these files needed and if so how are they organised in the VS2010 IDE?

    Thanks

    • Unzip the file and find the .sln file (visual studio solution file). Double click on this file to open the entire project in VS2010. All files can then be viewed in the solution explorer which is typically on the right side of the VS2010 IDE. Good Luck!

      • Great, thanks for that.

        I can see a few late nights working on this with my new found friend C#

        John

  2. 1st Late night!

    I had hoped not to come back so soon…………….

    download/unzipped/double click .sin file and get:

    ‘k:\webcam stuff\vsc#2010 webcam files\source\WebCamLib\WebCamLib.vcxproj’ cannot be opened because its project type (.vcxproj) is not supported by this version of the application.

    To open it, please use a version that supports this type of project.

    Click to MS general help says:

    “It is common for this error to occur when attempting to open a project file that cannot be opened with the installed version of Visual Studio. For example, attempting to open a Visual C# project file with Visual Basic 2005 Express Edition will open this dialog box”.

    In my case, I am using Visual C# 2010 Express, is that the problem?

    Is there a work around?

    Many thanks
    John

  3. Is there any way you could compile the WebCamLib.dll in release mode? Maybe just re-upload the code after the compile and the two different DLLs will be in the code.

      • Hi Jake,
        Is there a reason you have the C++ WebCamLib as a .NET 4.0 Framework? Could you not have it in an earlier version? I only ask because I am required to use 3.5 for my project.

        I am just asking.

      • This sucks 😦
        I tried to change it, but it just won’t go back to 3.5 and compile correctly. It really looked promising.

      • Rick,

        I think the problem is that you cannot target 3.5 in a C++ project that is going through the upgrade wizard in VS2010. (see link for details: http://blogs.msdn.com/b/jjameson/archive/2010/05/07/c-compiler-in-visual-studio-2010-must-target-net-framework-4.aspx)

        If you really need this to work in 3.5, my suggestion would be to:

        1. Down load the original source code from code project and compile the WebCamLib project in VS 2008. You should be able to produce a 3.5 targeted dll in this manner.

        2. Go back into the VS 2010 source code from here and remove the 4.0 targeted WebCamLib project from the solution.

        3. Go into both of the other VS 2010 projects (WebCamWrapper and Demo) and remove the reference to the WebCamLib project from the references folder.

        4. Go into both of the other VS 2010 projects (WebCamWrapper and Demo) and add a new reference to the 3.5 targeted WebCamLib.dll produced from the VS 2008 WebCamLib project compile.

        5. Go into the project properties for both VS 2010 projects (WebCamWrapper and Demo) and change the targeted framework to 3.5.

        I was going to be nice and do this for you, but I do not have VS 2008, and the original code project source code does not contain any dll’s. I just don’t feel installing VS 2008 on this machine either.

        Good luck!

  4. I’ve found that this will not run at all on anything that does not have Visual Studio installed. I checked on XP SP3 and Win7 SP1, both with the latest versions of the .NET framework. They all throw an error saying that it could not find WebCamLib.dll or one of its dependencies. This happens when you try to run the demo in the release package outright. How can I get this working properly on a computer that doesn’t have VS? I’ve integrated it into a project already and didn’t expect this kind of issue. I’d like to use this as it’s some pretty nice code that works wonderfully…but only when it actually does work. I’ve already tried a number of different ways to interface with a webcam in a C# project (p/invoke, blech) and don’t want to have to hunt down another.

    • My intent was only to get the project to compile in VS 2010 so people could view and test the demo in Debug. I used it for testing and lifted some of the code for other projects. I had no plans of deploying the demo app “as is”. The code was intentionally left as close to the original Code Project description as possible. Having said this, I took another look at the project for you. I think I have resolved your concern, and I added 2 new links to the post above. However, if my changes do not work, I am not sure I can be of further assistance. Demo.exe and all related dll’s are posting to the Demo/release folder now at least.

      • Hello Jake,

        sorry, I didn’t see your comment.
        I works now on my windwos 7 pc with this new dll.

        But on my Windows XP this issue is still remaining with the new dll.

        Do you have any idea ?

      • This is my best guess… I think this is related to the entire solution being compiled in framework 4.0 in VS 2010. In order to resolve this, I think that the C++ project in the solution would need to be compiled under framework 3.5 with x86, and then the dll would need to be placed in the VS 2010 project with both C# projects compiled under target frame work 4.0. I could not get the C++ project to compile in framework 3.5 using VS 2010 though. Someone else would have to do this.

        If .net framework 4.0 is not on the machine, I don’t think it will run without including an installer with the 4.0 framework in the project.

      • Hello Jake,
        I compiled the weCamLib in VS 2008 with .net Framework 3.5 with x86 but still the same error on the xp machine (with .net 4 runtime).
        What did you change in Version 2 in order to make it run on Windows 7 ?
        One question besides: the webcamLib.dll only has to be referenced in the WebCamWrapper Porject right ? Im my application itself I don’t have to reference the webcamLib.dll, only
        Touchless.Vision.dll.

      • yes, .net famework 3.5 SP1 is installed on the xp machine. And .net framework 4.0 (client profile and extended).

      • Hello Jake,

        yes .net 3.5 is installed. And also 4.0 Client Profile and Extended.

        Do you have any ideas ? Does it work for you on XP ?

      • I have never tried to run the project on xp. You might check the comments on code project too. I’m sure someone has experienced this before possibly?

      • Hello Jake,

        I use InnoSetup as an installer but that didn’t help.
        I havent tried it with the visual sudio setup project yet but
        I will try.

      • Hello Jake,

        it must be somehow system depending, because I have an xp machine SP3 professional edition 32 bit where it works and two where it does not ?

        On the machine where it works I also use the webCamLib compiled in visual studio 2010 with .net 4.0. So this seems not to be the problem.

        this is the error message of the problem:

        Could not load file or assembly ‘WebCamLib.dll’ or one of its dependencies. Das angegebene Modul wurde nicht gefunden. at Touchless.Vision.Camera.CameraService.d__0.MoveNext()
        at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
        at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
        at Touchless.Vision.Camera.CameraService.get_AvailableCameras()

      • That’s great news!!! I am surprised that the install program would not pick that up as a dependency. At least you figured out what it was 🙂

      • Thanks matthias, I also have the same issue. The c++ runtime was not installed.

        And Thanks jakemdrew for your support and code

    • Hello Noble,

      this must have something to do with the compilation of the webCamLib.
      When I open the dll in Dependency walker it gives the following error:

      Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.

      and it points to ieframe.dll

      • I finally got to the point of testing this and I am getting the same issue. I am getting a File not found error. When I run dependency walker it is showing ieframe is missing. Please tell me I am not crazy on this.
        This happens on both 32 and 64 bit version of windows.

  5. Thanks Jake for the details, I actually tried that but there is an issue with the reference, it really just doesn’t play nicely with 3.5. In fact wit your wrapper having the xaml file in it, it seems to have issues with that also.

    • If you have a copy of the WebCamLib.dll targeted to 3.5, post a link and I will see what I can do. Based on your comment though, I might not have much luck either.

      • I think I might be able to get my client app working in 4.0, I had to switch a DLL and now it look like I can make it work.

  6. Hi Jake,
    I have been playing around with the code and I think I have run into a bug. I have my picture box set to stretch so whatever the size of the captured screen is shown in the window. The issue is that it is not allowing the captured frame to stretch.

    A then decided to change the below properties:
    _frameSource.Camera.CaptureWidth = 260;
    _frameSource.Camera.CaptureHeight = 260;

    But even know I did that, it still sets them back to the default values of
    _frameSource.Camera.CaptureWidth = 640;
    _frameSource.Camera.CaptureHeight = 480;

    as soon as the
    _frameSource.StartFrameCapture();
    line is hit.

    Any idea what I can do about this?

  7. I have been doing some digging, it looks like it is because you are assigning the _latestFrame to frame.Image which at the beginning does not have any of the properties that were just set.

  8. I figured it out!
    In drawLatestImage I changed the code from:

    e.Graphics.DrawImage(_latestFrame, 0, 0, _latestFrame.Width, _latestFrame.Height);

    To

    pictureBoxDisplay.Image = _latestFrame;

    Now I can use the PictureBox properties.

    • Check previous comments from “Rick Toner” below for more details.

      Here was his resolution:
      “In drawLatestImage I changed the code from:

      e.Graphics.DrawImage(_latestFrame, 0, 0, _latestFrame.Width, _latestFrame.Height);

      To

      pictureBoxDisplay.Image = _latestFrame;

      Now I can use the PictureBox properties.”

      • so

        pictureBoxDisplay.Image = _latestFrame;
        pictureBoxDisplay.Size = new Systems.Drawing.Size(this.MainForm.ActiveForm.Size.Width,this.MainForm.ActiveForm.Size.Height);

        should make the video appear as full screen?

    • Rick Toner (above) has reported no problems with screen re-size following his instructions. If you are following the steps he describes, I am not sure where you are getting your extra lines of code from in your example above? It looks to me like he is only changing one line of code in the mainform.cs module (line 83). If you want the panel to resize, try changing the sizemode on the panel to “stretchimage” as rick describes in his first post.

      • Also, it looks like (based on original post) that Rick might still be making the calls:

        _frameSource.Camera.CaptureWidth = 260; //desired width here
        _frameSource.Camera.CaptureHeight = 260; //desired height here

        To re-size the capture image.

  9. Hi Jake,

    I am tried to build this project for X64 platform, I am having problems when tried. Can you please post a X64 version of project and upload. this would help a lot.

    friendly,
    S.

    • Hello Paul,

      As far as I know, the supporting code libraries which this project depends on (specifically DShow.NET) will not compile in X64. The code would need to be updated for that platform. DirectShow is a pretty common library, and you might be able to find an x64 version now, if you looked around for it. Please let me know, if you come across one!

      Thanks,
      Jake

  10. Gorgeous work, Jake!

    I’m a newbie to C# and your article and source code help me a lot 😀 Finally, it pretty works on my .NET Framework 3.5 SP1 win7 x86 .

    Many thanks,
    Binh

  11. Thanks so much!
    I was halfway through the process of fixing that “versatile” project when I discovered this post. It may seem radical, but I very much like downloading code that builds.

    Thanks again, and hopefully this post gets a little higher pagerank.

    Camera, C#, Webcam, Directshow, Touchless, Touchless.Vision.dll

  12. Hi.
    I have one problem … I reuse libraries to write my program, only with features I need. On win7 it works great (both versions), but on win XP it wont find any camera. Any ideas?

    • I vaguely remember reading that others had targeted earlier versions of the .net framework within the project to get things working on XP. You may search the earlier comments here. Best of luck and sorry for the late reply!

  13. HI Jake, i have a problem. When i want to change some things in code it won’t react. Even if i delete all lines from some class program debug normal and i can use it. I don’t know what i need to do to fix this. By the way only thing what i want to do is to change resolution of picture to 640×360 and implement code in my program

Leave a reply to jakemdrew Cancel reply