public Image TakePicture() { if (m_handle == IntPtr.Zero) { throw new ObjectDisposedException("WebCam"); } IDataObject oldData = Clipboard.GetDataObject(); try { if (ApiFunctions.SendMessage(m_handle, CaptureMessage.WM_CAP_EDIT_COPY, IntPtr.Zero, IntPtr.Zero) == IntPtr.Zero) { throw new InvalidOperationException("Error copying image to clipboard"); } if (!Clipboard.ContainsImage()) { throw new InvalidOperationException("No image on clipboard"); } return(Clipboard.GetImage()); } finally { Clipboard.SetDataObject(oldData); } }
public void Stop() { if (m_handle != IntPtr.Zero) { ApiFunctions.SendMessage(m_handle, CaptureMessage.WM_CAP_DRIVER_DISCONNECT, IntPtr.Zero, IntPtr.Zero); ApiFunctions.DestroyWindow(m_handle); m_handle = IntPtr.Zero; } GC.SuppressFinalize(this); }
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."); } }