Gopal's Tech Blog

Programming, Engineering, Science and Maths

Controlling a web camera using C#

Posted by Gopal on October 27, 2009

From the C# programming language, controlling a web camera in windows computer is not a hard task. The .Net Framework bridges the gap between Native language and Managed language. The web camera of your computer works in the Hardware level of the computer to capture photos and record videos. It requires an advanced programming knowledge to directly access the camera, but there is an easy way to control the web camera by P/Invoking a native DLL. You can P/Invoke a DLL written in C/C++ and use its features in C# (Please refer to this article for the tutorial of P/Invoking). In this article I am using the same method to control a web camera.

webcam_client

Download Source Code

  • First of all you have to include the using System.Runtime.InteropServices directive in you list of usings. Paste the following code in your application.

//Call Native DLLs

[System.Runtime.InteropServices.DllImport("avicap32.dll")]
static extern bool capGetDriverDescriptionA(short wDriverIndex, string lpszName, int cbName, string lpszVer, int cbVer);

[System.Runtime.InteropServices.DllImport("avicap32.dll")]
static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWnd, int nID);

[System.Runtime.InteropServices.DllImport("avicap32.dll")]
static extern int capCreateCaptureWindow(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWnd, int nID);

[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SendMessageA")]
static extern int SendMessage(int hwnd, int Msg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);

[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetWindowPos")]
static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);

[System.Runtime.InteropServices.DllImport("user32")]
static extern bool DestroyWindow(int hndw);

const int WM_CAP_START = 1024;
const int WS_CHILD = 1073741824;
const int WS_VISIBLE = 268435456;
const int WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10);
const int WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11);
const int WM_CAP_EDIT_COPY = (WM_CAP_START + 30);
const int WM_CAP_SEQUENCE = (WM_CAP_START + 62);
const int WM_CAP_FILE_SAVEAS = (WM_CAP_START + 23);
const int WM_CAP_SET_SCALE = (WM_CAP_START + 53);
const int WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52);
const int WM_CAP_SET_PREVIEW = (WM_CAP_START + 50);
const int SWP_NOMOVE = 2;
const int SWP_NOSIZE = 1;
const int SWP_NOZORDER = 4;
const int HWND_BOTTOM = 1;

//End Calling native DLLs

  • Drag a Picture box control into the designer, size:320×240, don’t change its name property.
  • Add a button control, button1. Set Its onClick event handler to following code.

private void button1_Click(object sender, EventArgs e)
{
string picFileName = “picture”;
IDataObject data;
Image bmap;
//—copy the image to the clipboard—
SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0);
//—retrieve the image from clipboard and convert it
// to the bitmap format
data = Clipboard.GetDataObject();
if (data == null)
{
return;
}

if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
{
//—convert the data into a Bitmap—
bmap = ((Image)(data.GetData(typeof(System.Drawing.Bitmap))));

if (bmap != null)
{

Image newBmap = bmap.GetThumbnailImage(200, 150, null, System.IntPtr.Zero);

newBmap.Save(“@C:\Pic1.jpg”, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}

  • Add another method to preview the video inside the Picture box. Also remember to create two global variables   private int hWnd and  private int hWndC, but don’t set any values for the variables.

private void PreviewVideo(PictureBox pbCtrl)
{
hWnd = capCreateCaptureWindowA(“0″, WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, pbCtrl.Handle.ToInt32(), 0);
if (SendMessage(hWnd, WM_CAP_DRIVER_CONNECT, 0, 0) != 0)
{
SendMessage(hWnd, WM_CAP_SET_SCALE, 1, 0);
SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0);
SendMessage(hWnd, WM_CAP_SET_PREVIEW, 1, 0);
SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, pbCtrl.Width, pbCtrl.Height, SWP_NOMOVE | SWP_NOZORDER);
}
else
{
//Could not connect to camera
DestroyWindow(hWnd);
}
}

  • Finally, call the PreviewVideo(pictureBox1) method when the application loads.

private void Form1_Load(object sender, EventArgs e)
{
PreviewVideo(pictureBox1);
}

Download Source Code

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>