We've been assigned a task at school, where we are to implement a piece of software that communicates via Object Oriented Network Programming... So we thought about what we could do, and decided we should take a look at controlling a webcam from another location. Hence I wanted to try viewing a webcam with C# and the .NET Framework 2.0.

I searched the net, where I found a great article at:
http://msdn.microsoft.com/coding4fun/someassemblyrequired/lookatme/default.aspx

So I started a new project, and wanted to include the Windows Image Acquisition dll wiaaut.dll (http://www.microsoft.com/downloads/details.aspx?FamilyID=a332a77a-01b8-4de6-91c2-b7ea32537e29&DisplayLang=en), should be quite simple to follow the instruction:
"... via "Add Reference" from with Visual Studio.NET 2005 and a .NET wrapper will be automatically generated.". Somehow it wasn't that simple. I got the error telling me that I should make sure it was a correct dll or COM component. So googled that, and found a tool from Microsoft to convert it to a CLR assembly (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpgrftypelibraryimportertlbimpexe.asp). Ran the command: tlbimp wiaaut.dll (From VS.NET command prompt), and it created a file called WIA.dll. This I could import, and found myself working up a UI, in order to control the webcam.

I created a class, to control the webcam, and mostly copied the solution from Coding4Fun.

It was then time to run the app, and try to get the CommonClassDialog to appear, for me to select a webcam. Again, no luck. Got the error saying:
Class Not Registered or Error 80040154, in the line:

Device d = commonDialog.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);

So, googleeeeeed that, and came up with the idea to try an register the component wiaaut.dll, since I got an error stating it wasn't registered. Brilliant, right?
Ran the command: regsvr32 wiaaut.dll
What do ya' know - it worked. I was able to retrieve a list of Webcams and Scanners from Windows, and select the one I wanted to use. I could now take pictures with my webcam.

But wait - where's the video???
No video wasn't good. We want to send video through the network, but that is not really possible, when I only have pictures... Send loads of pictures, in stead of video stream? I think not

When I was searching about, a site poped up, from codeproject:
http://www.codeproject.com/dotnet/wiascriptingdotnet.asp
A brilliant article describing how to enable video in your application. So there I was - add it again.

Created a new class and copied most of the main program from the solution above. Everything seemed to be okay, but when I compiled, it came with an error saying it couldn't convert System.Windows.Forms.Panel to WIAVIDEOLib._RemotableHandle. So what to do?
I searched google for answers, and found a page stating that you could Marshal the Handle to another pointer type:
http://www.vbforums.com/showthread.php?t=379485

Create the method private WIAVIDEOLib._RemotableHandle getRemoteHandle(System.IntPtr ptr), and return the handle of the Panel to WIAVIDEOLib._RemotableHandle.

This was succesfull, in an absurd way. When starting the program, clicking "Start video", the video appeared in a new ActiveVideo window. I was of course celebrating for a minute, but I couldn't make the video appear in the correct window. I've read that the codeproject way, used a panel to overlay the video on, but instead it appeared in a new window.
Since his code worked with applying a windows control to the method, I tried deleting the reference I added from windows, and added the dll from his project - Answer: Succes!

Yehaa, I created my first C# Webcam application, which is like a pre alpha release.

Feel free to contact me, if you have any questions or comments!

WebCamControl.zip (207.61 KB)