Home > .Net, .Net 3.0 / 3.5, Windows Forms > WebCam in your applications using C#

WebCam in your applications using C#

In the current project I am working I got a chance to implement web cam image capture. There is two different ways to do this in C#.

  1. Using WIA Sdk – It is library coming with Windows. And you can download this COM library from Microsoft site. But the problem of this approch is sometime the cam is not supported by WIA and you will get an exception like “Exception from HRESULT: Ox80210015″.
  2. Using AVICAP32 library – In this method we are invoking methods from avicap32.dll, and interacting with Web Cam. Here is a simple implementation, which will connect with WebCam, show Preview and Save the Image.

    Implementation

    Add a Picturebox to a Form, which will display the Preview. And write the following code.

    
    [DllImport("user32", EntryPoint = "SendMessage")]
    public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
    
    [DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowA")]
    public static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID);
    
    public const int WM_USER = 1024;
    public const int WM_CAP_CONNECT = 1034;
    public const int WM_CAP_DISCONNECT = 1035;
    public const int WM_CAP_GET_FRAME = 1084;
    public const int WM_CAP_COPY = 1054;
    
    public const int WS_CHILD = 0x40000000;
    public const int WS_VISIBLE = 0x10000000;
    
    public const int WM_CAP_START = WM_USER;
    public const int WM_CAP_SET_SCALE = WM_CAP_START + 53;
    public const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;
    public const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
    
    private int mCapHwnd = 0;	//Handle
    
    private void Connect()
    {
    
    	mCapHwnd = capCreateCaptureWindowA("WebCap", WS_VISIBLE | WS_CHILD, 0, 0, 350, 350, this.pictureBox.Handle.ToInt32(), 0);
    	if (SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0) > 0)	//This will make sure the device is connected to the System
    	{
    		SendMessage(mCapHwnd, WM_CAP_SET_PREVIEWRATE, 30, 0);
    		SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 1, 0);
    	}
    	else
    	{
    		MessageBox.Show("No device found!");
    	}
    }
    
    private void TakePicture()
    {
    
    	SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0);
    	SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0);
    	//Saving the Clipborad Data
    	using (SaveFileDialog dlg = new SaveFileDialog())
    	{
    		dlg.Filter = "JPG Images|*.jpg";
    		dlg.OverwritePrompt = true;
    		dlg.ValidateNames = true;
    		dlg.DefaultExt = "JPG";
    		if (dlg.ShowDialog(this) == DialogResult.OK)
    		{
    			Clipboard.GetImage().Save(dlg.FileName, ImageFormat.Jpeg);
    		}
    	}
    }
    private void Disconnect()
    {
                SendMessage(mCapHwnd, WM_CAP_DISCONNECT, mCapHwnd, 0);
    }
    

    On the Form_Load event, call the Connect() method, which will connect to the Web cam and display a Preview in the Picturebox. I have added a button to take picture and on the Click event of the button, call the TakePicture() method, which will grabs the current frame from Web Cam and copy it to the Clipboard, and using the FileSaveDialog it is saving to the FileSystem. On the Form_Closing() event, call the Disconnect() method, which disconnects the cam. Happy Programming :)

  3. You can download WIA library from : http://www.microsoft.com/downloads/details.aspx?familyid=a332a77a-01b8-4de6-91c2-b7ea32537e29.

  1. No comments yet.
  1. No trackbacks yet.