如何从网络摄像头捕获video?

我需要从网络摄像头捕获video。 C#/ .NET中是否有任何类可以帮助我解决这个问题。 我只对实时数据感兴趣。

是否有任何好的C#/ .NET书籍我可以学习以获得对语言和平台的深入了解?

这就是我使用的。 您需要第一个类来迭代您的设备:

public class DeviceManager { [DllImport("avicap32.dll")] protected static extern bool capGetDriverDescriptionA(short wDriverIndex, [MarshalAs(UnmanagedType.VBByRefStr)]ref String lpszName, int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref String lpszVer, int cbVer); static ArrayList devices = new ArrayList(); public static TCamDevice[] GetAllDevices() { String dName = "".PadRight(100); String dVersion = "".PadRight(100); for (short i = 0; i < 10; i++) { if (capGetDriverDescriptionA(i, ref dName, 100, ref dVersion, 100)) { TCamDevice d = new TCamDevice(i); d.Name = dName.Trim(); d.Version = dVersion.Trim(); devices.Add(d); } } return (TCamDevice[])devices.ToArray(typeof(TCamDevice)); } public static TCamDevice GetDevice(int deviceIndex) { return (TCamDevice)devices[deviceIndex]; } } 

和这一个控制凸轮。

 public class TCamDevice { private const short WM_CAP = 0x400; private const int WM_CAP_DRIVER_CONNECT = 0x40a; private const int WM_CAP_DRIVER_DISCONNECT = 0x40b; private const int WM_CAP_EDIT_COPY = 0x41e; private const int WM_CAP_SET_PREVIEW = 0x432; private const int WM_CAP_SET_OVERLAY = 0x433; private const int WM_CAP_SET_PREVIEWRATE = 0x434; private const int WM_CAP_SET_SCALE = 0x435; private const int WS_CHILD = 0x40000000; private const int WS_VISIBLE = 0x10000000; [DllImport("avicap32.dll")] protected static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent, int nID); [DllImport("user32", EntryPoint = "SendMessageA")] protected static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam); [DllImport("user32")] protected static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); [DllImport("user32")] protected static extern bool DestroyWindow(int hwnd); int index; int deviceHandle; public TCamDevice(int index) { this.index = index; } private string _name; public string Name { get { return _name; } set { _name = value; } } private string _version; public string Version { get { return _version; } set { _version = value; } } public override string ToString() { return this.Name; } ///  /// To Initialize the device ///  /// Height of the Window /// Width of the Window /// The Control Handle to attach the device public void Init(int windowHeight, int windowWidth, int handle) { string deviceIndex = Convert.ToString(this.index); deviceHandle = capCreateCaptureWindowA(ref deviceIndex, WS_VISIBLE | WS_CHILD, 0, 0, windowWidth, windowHeight, handle, 0); if (SendMessage(deviceHandle, WM_CAP_DRIVER_CONNECT, this.index, 0) > 0) { SendMessage(deviceHandle, WM_CAP_SET_SCALE, -1, 0); SendMessage(deviceHandle, WM_CAP_SET_PREVIEWRATE, 0x42, 0); SendMessage(deviceHandle, WM_CAP_SET_PREVIEW, -1, 0); SetWindowPos(deviceHandle, 1, 0, 0, windowWidth, windowHeight, 6); } } ///  /// Shows the webcam preview in the control ///  /// Control to attach the webcam preview public void ShowWindow(global::System.Windows.Forms.Control windowsControl) { Init(windowsControl.Height, windowsControl.Width, windowsControl.Handle.ToInt32()); } ///  /// Stop the webcam and destroy the handle ///  public void Stop() { SendMessage(deviceHandle, WM_CAP_DRIVER_DISCONNECT, this.index, 0); DestroyWindow(deviceHandle); } } 

ShowWindow将PictureBox作为参数。

我建议你使用第三方库。 这将是最好的解决方案,而不是发明自己的自行车。 在这里,我使用了AForge.Net 。 虽然它在性能方面存在一些问题,但是当我的性能成为一个关键问题时,我自己调整了库。 AForge.Net代码是开源的,您可以根据自己的需要进行调整。

至于书籍,你绝对应该看看Jeffrey Richter的“CLR via C#”和John Skeet的“C#in Depth” 。

您可以使用Microsoft Expression Encoder 4 SP2 。 这是一个很棒的库,您可以将其添加到项目中并获取实时预览,快照和video录制 。 更多细节包括Massimo Conti的示例项目: 如何在C#中使用带有.NET Framework 4.0和Microsoft Expression Encoder 4的Web摄像头

使用WebCam_Capture.dll捕获照片和video。 这里链接是链接的源代码