public WebCam(int deviceIndex, IntPtr parentHandle, int width, int height, bool showWindow) { m_width = width; m_height = height; m_index = deviceIndex; m_parentHandle = parentHandle; if (m_parentHandle == IntPtr.Zero) { throw new NullReferenceException("parentHandle cannot be 0"); } WindowStyle windowStyle = WindowStyle.WS_CHILD; if (showWindow) { windowStyle |= WindowStyle.WS_VISIBLE; } m_handle = ApiFunctions.capCreateCaptureWindow("WebCam " + deviceIndex, windowStyle, 0, 0, width, height, parentHandle, 0); if (m_handle == IntPtr.Zero) { throw new InvalidOperationException("Error creating camera window"); } // Initialize Camera if (ApiFunctions.SendMessage(m_handle, CaptureMessage.WM_CAP_DRIVER_CONNECT, new IntPtr(deviceIndex), IntPtr.Zero) == IntPtr.Zero) { throw new InvalidOperationException("Error connecting to camera"); } // Enable preview mode, ensuring our callback will be called if (ApiFunctions.SendMessage(m_handle, CaptureMessage.WM_CAP_SET_SCALE, new IntPtr(-1), IntPtr.Zero) == IntPtr.Zero) { throw new InvalidOperationException("Error disabling scaling"); } if (ApiFunctions.SendMessage(m_handle, CaptureMessage.WM_CAP_SET_PREVIEWRATE, new IntPtr(100), IntPtr.Zero) == IntPtr.Zero) { throw new InvalidOperationException("Error setting preview rate"); } if (ApiFunctions.SendMessage(m_handle, CaptureMessage.WM_CAP_SET_PREVIEW, new IntPtr(-1), IntPtr.Zero) == IntPtr.Zero) { throw new InvalidOperationException("Error enabling preview mode."); } }