C# 从网络摄像头捕获图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14677880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# capturing image from webcam
提问by Viktor Kirilov
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.
过去两天,我一直在寻找一种使用 C# 从网络摄像头捕获图像的方法。我是 C# 新手,我不想使用外部第三方库,所以我找到了两种不错的方法,但两者似乎都返回了几乎相同的错误。我无法让他们中的任何一个工作,所以如果你帮我让他们中的一个运行或帮我找到替代方案,那将是非常好的。
So the first way I found is using Windows WIA. I found the following code:
所以我找到的第一种方法是使用 Windows WIA。我找到了以下代码:
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:
我发现的第二种方法是将 Avicap32.dll 与以下示例一起使用:
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.
以下异常: NullReferenceException:未将对象引用设置为对象的实例。
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.
我认为这两种解决方案都造成了问题,因为他们找不到我的相机,但我可以在 Skype 中使用我的相机而没有任何问题。
回答by Roman R.
- 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
- WIA 用于静止图像,它是一种“与扫描仪配合使用的 API”;
0x80210015
是WIA_S_NO_DEVICE_AVAILABLE
- AVICAP32 API 名称是“Video for Windows”,它确实已被弃用且已过时,它可能会奏效(兼容性仍然存在)但很有可能它不会带你去任何地方
APIs to work with web cameras are:
与网络摄像头配合使用的 API 包括:
- 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:
两者都是本机 API,您可能很难直接从 C# 代码中连接它们,但是使用DirectShow.NET(尤其是)和 Media Foundation.NET,您可以使用托管代码的包装器。您可以在此处找到有关使用 DirectShow.NET 的更多信息:
回答by Gary Kindel
I recommend Aforge.net framework.
我推荐Aforge.net 框架。
It was able to implement videoCaptureDevice classused in the sample project: Snapshot Maker exampleto 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 Panelbut it is stable and it is easy method for setting video and image resolutions from a supported device.
它能够实现示例项目中使用的videoCaptureDevice 类:Snapshot Maker 示例以快速创建图像捕获对话框。它比DirectShow 库慢一点 - 如何使用 directshow 库捕获图像而不在 PictureBox 或面板上显示网络摄像头实时图像,但它很稳定,并且是从支持的设备设置视频和图像分辨率的简单方法。
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.
我遇到的唯一问题是 VideoCaptureDevice.SimulateTrigger() 使用 bacjground 线程从视频源创建图像并在事件中返回图像。如果将返回的图像放在 UI 线程上的 winform 控件上,则需要委托方法来防止跨线程问题。
Get the source from Snapshot Maker project from Aforge.net source SVN link.
从 Aforge.net 源SVN 链接获取来自 Snapshot Maker 项目的源代码。
回答by Ashitakalax
I tried multiple approaches, and the easiest approach for me was Emgu.cv(nuget package).
我尝试了多种方法,对我来说最简单的方法是 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)
就是这样(从 API 版本 3.3.0 开始)
Old API Approach
旧的 API 方法
Capture capture = new Capture(); //create a camera captue
Bitmap image = capture.QueryFrame().Bitmap; //take a picture