mercredi 16 avril 2014

C#, capture d'image de webcam - Stack Overflow


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.




It is worth mentioning you already asked the Q before Easiest way to capture image with webcam and save it to file


On your progress:



  • WIA is for stills, it's a sort of "API to work with scanners"; 0x80210015 is WIA_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:




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.



It is worth mentioning you already asked the Q before Easiest way to capture image with webcam and save it to file


On your progress:



  • WIA is for stills, it's a sort of "API to work with scanners"; 0x80210015 is WIA_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:



0 commentaires:

Enregistrer un commentaire