C# capturing image from webcam
Solution 1
I tried multiple approaches, and the easiest approach for me was Emgu.cv(nuget package).
VideoCapture capture = new VideoCapture(); //create a camera capture
Bitmap image = capture.QueryFrame().Bitmap; //take a picture
That's it (as of API version 3.3.0)
Old API Approach
Capture capture = new Capture(); //create a camera captue
Bitmap image = capture.QueryFrame().Bitmap; //take a picture
Solution 2
- WIA is for stills, it's a sort of "API to work with scanners";
0x80210015
isWIA_S_NO_DEVICE_AVAILABLE
- AVICAP32 API name is "Video for Windows" which is really deprecated and obsolete, it might work out (compatibility is still here) but chances are high that it will take you nowhere
APIs to work with web cameras are:
- DirectShow
- Media Foundation
Both are native APIs and you might have hard time to interface them directly from C# code, however with DirectShow.NET (especially) and Media Foundation.NET you have wrappers for managed code. You can find more on using DirectShow.NET here:
- Capturing frames from webcam using DirectShow.NET
- DirectShow Library-How to capture image using directshow library without showing the webcam live images on the PictureBox or Panel
- Get image from webcam
Solution 3
I recommend Aforge.net framework.
It was able to implement videoCaptureDevice class used in the sample project: Snapshot Maker example to quickly create a image capturing dialog. It is a little slower than DirectShow Library-How to capture image using directshow library without showing the webcam live images on the PictureBox or Panel but it is stable and it is easy method for setting video and image resolutions from a supported device.
The only issue I ran into is VideoCaptureDevice.SimulateTrigger() uses a bacjground thread to create an image from a video feed and returns the image on an event. Need to delegate method to prevent cross thread issues if you place the returned image on a winform control on the UI thread.
Get the source from Snapshot Maker project from Aforge.net source SVN link.
Viktor Kirilov
Updated on January 20, 2020Comments
-
Viktor Kirilov over 4 years
Last two days I've been looking for a way to capture an image from the webcam using C#. I'm pretty new in c# and I DO NOT want to use external third party libs, so I found two nice ways, but both seems to return almost the same error. I couldn't get any of them to work, so it would be really nice if you help me to get one of them running or help me to find alternative.
So the first way I found is using 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 exacly what I'm looking for, but I can't get it running, because I'm getting the following error on the second line:
Exception from HRESULT: 0x80210015
The second way I found is using the Avicap32.dll with the following sample:
http://www.timvw.be/wp-content/code/csharp/testavicap32.zip
but I'm getting on this code:
Image image = ((CaptureDevice)cboDevices.SelectedItem).Capture(); image.Save(@"c:\capture.png", ImageFormat.Png);
the following exception: NullReferenceException: Object reference not set to an instance of an object.
I think that both solution are causing problems because they can't find my camera, but I can use my camera in skype without any problems.
-
MikeT over 8 yearsi Succeeded using them too, however their level of comparability for stills seems rather low, I'm running at only 2 out of 7 tested cameras, my guess is that modern cameras don't bother with a separate image capture function and just grab a video frame, and aforge hasn't been updated in several years
-
André Boonzaaijer almost 7 yearsTried it but should now be VideoCapture instead of Capture.
-
Ashitakalax almost 7 yearsI agree, truthfully this question Title should be restructured to be more specific. The body of the question requests for an approach without 3rd party libraries. But this question is the only one relating to c# webcam image capture.
-
Ashitakalax over 5 years@Termatinator It is just a bitmap at this point so you can treat it like any image. here is a link with how to. stackoverflow.com/questions/12909905/saving-image-to-file
-
Marcello B. over 5 yearsEmgu.cv ended up corrupting my solution when I would try to open the designer
-
stefan.seeland about 5 yearsDon't forget to dispose capture and image.
-
shen almost 4 yearsAnswer is from 2-3 years ago. have core libraries advanced since then?
-
Roman R. almost 4 years@AnonymousType this answer has been valid for ~10 years if not more, that is this is pretty much stabilized. The recent updates are mostly related to UWP development:
Windows.Media.Capture
and friends. This new stuff is mostly higher level wrapping over mentioned Media Foundation with interface into UWP world. Capture photos and video with the Windows built-in camera UI -
shen almost 4 yearsThanks @Roman R. I read that. Ended up going with OpenCV
-
Eric Bourque about 3 yearsAlso get package Emgu.CV.Bitmap and use capture.QueryFrame().ToBitmap;