C # capture webcam image

For the past two days, I have been looking for a way to capture a webcam image using C #. I am new to C # and I DO NOT want to use external third-party libraries, so I found two nice ways, but both return almost the same error. I could not get them to work, so it would be very nice if you could help me get one of them to flee or help me find an alternative.

So, the first way I found is to use Windows WIA. I found the following code:

CommonDialogClass dialog = new CommonDialogClass(); Device camera = dialog.ShowSelectDevice(WiaDeviceType.CameraDeviceType, true, false); // take the photo Item item = camera.ExecuteCommand(CommandID.wiaCommandTakePicture); ImageFile image = (ImageFile)item.Transfer(FormatID.wiaFormatJPEG); // filename and saving image.SaveFile("Test.jpg"); 

this code seems to be what I'm looking for, but I cannot run it because the following error appears in the second line:

 Exception from HRESULT: 0x80210015 

The second way I found is to use Avicap32.dll with the following example:

 http://www.timvw.be/wp-content/code/csharp/testavicap32.zip 

but I get this code:

 Image image = ((CaptureDevice)cboDevices.SelectedItem).Capture(); image.Save(@"c:\capture.png", ImageFormat.Png); 

the following exception: NullReferenceException: The object reference is not set to the object instance.

I think both solutions cause problems because they cannot find my camera, but I can use my camera in skype without any problems.

+7
source share
3 answers
  • WIA for still images, this is a kind of "API for working with scanners"; 0x80210015 WIA_S_NO_DEVICE_AVAILABLE
  • The AVICAP32 API name is β€œVideo for Windows,” which is really deprecated and deprecated, it might work (compatibility is still here), but the chances are high that it will lead to nothing.

Webcam API:

  • Directshow
  • Media fund

Both are built-in APIs, and it may be difficult for you to associate them directly with C # code, however DirectShow.NET (especially) and Media Foundation.NET you have wrappers for managed code. You can find more about using DirectShow.NET here:

+9
source

I tried several approaches, and the easiest way for me was Emgu.cv (nuget package).

  VideoCapture capture = new VideoCapture(); //create a camera capture Bitmap image = capture.QueryFrame().Bitmap; //take a picture 

What is it (from API version 3.3.0)

Old API Approach

  Capture capture = new Capture(); //create a camera captue Bitmap image = capture.QueryFrame().Bitmap; //take a picture 
+7
source

I recommend the Aforge.net framework .

He was able to implement the videoCaptureDevice class used in the sample project: An example of creating a snapshot to quickly create an image capture dialog. This is a bit slower than the DirectShow Library. How to capture an image using the directshow library without showing live webcam images on the PictureBox or panel , but it is stable and this is an easy way to adjust the resolution of the video and image from a supported device.

The only problem I encountered is VideoCaptureDevice.SimulateTrigger () uses the bacjground stream to create an image from the video stream and returns the image in the event. You must delegate the method to prevent cross-threading issues if you place the return image in a winform control in a user interface thread.

Get the source from the Snapshot Maker project from the Aforge.net source SVN link .

+1
source

All Articles