using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; namespace WebCam { class WebCamFeed { #region Constants private const int WS_CHILD = 0x40000000; private const int WS_VISIBLE = 0x10000000; private const int WM_CAP_DRIVER_CONNECT = WM_USER + 10; private const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11; private const int WM_CAP_SET_PREVIEW = WM_USER + 50; private const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52; private const short WM_USER = 0x0400; private const long WM_CAP_SEQUENCE = WM_USER + 62; #endregion #region API Declaration [DllImport("user32.dll", EntryPoint = "SendMessageA")] static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); [DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowA")] static extern int capCreateCaptureWindow(string lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID); [DllImport("avicap32.dll", EntryPoint = "capGetDriverDescriptionA")] static extern int capGetDriverDescription(int wDriverIndex, string lpszName, int cbName, string lpszVer, int cbVer); #endregion #region Variables private Boolean isRunning = false; private String devName = ""; private int hWndC; private int outputHeight = 274; private int outputWidth = 235; private int frameRate = 29; #endregion public WebCamFeed() { //Do nothing! } public void initCam(IntPtr hWndParent) { if (isRunning == false) { //Create a capture window this.hWndC = capCreateCaptureWindow(devName, WS_CHILD | WS_VISIBLE, 0, 0, outputHeight, outputWidth, hWndParent,0); } else { MessageBox.Show("The camera is already running", "Camera Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } //Start the camera if (startCam(hWndParent) == false) { MessageBox.Show("Unable to initialise camera", "Camera Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } //Partial code taken from Pino's iCam Class public Boolean startCam(IntPtr hWndParent) { //Try to connect the camera driver if (SendMessage(hWndParent, WM_CAP_DRIVER_CONNECT, 0, 0) == 1) { //Start video preview SendMessage(hWndParent, WM_CAP_SET_PREVIEWRATE, frameRate, 0); SendMessage(hWndParent, WM_CAP_SET_PREVIEW, 1, 0); //Camera is running this.isRunning = true; //Return true (indicating that the camera did start) return true; } else { this.isRunning = false; //Return false (indicating that the camera didn't start) return false; } } public void resetCam(IntPtr hWndParent) { //Close camera stopCam(hWndParent); //Start camera again initCam(hWndParent); } public Boolean stopCam(IntPtr hWndParent) { //Stop the camera if (SendMessage(hWndParent, WM_CAP_DRIVER_DISCONNECT, 0, 0) == 1) { //Camera is now not running this.isRunning = false; //Return true (indicating that the camera did stop) return true; } else { //Give the user an error message MessageBox.Show("Unable to stop camera", "Camera Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //Return false (indicating that the camera didn't stop) return false; } } public int getFPS() { //Return the FPS return (1000 / frameRate); } public Boolean getIsRunning() { //Return 'isRunning' return isRunning; } } }