Using system;
Using system. runtime. interopservices;
Namespace webcam
{
/**/
///
/// Summary of avicap.
///
Public class showvideo
{
// Showvideo CILS
[Dllimport ("avicap32.dll")]
Public static extern intptr capcreatecapturew.wa (byte [] lpszwindowname, int dwstyle, int X, int y, int nwidth, int nheight, intptr hwndparent, int NID );
[Dllimport ("avicap32.dll")]
Public static extern bool capgetdriverdescriptiona (short wdriver, byte [] lpszname, int cbname, byte [] lpszver, int cbver );
[Dllimport ("user32.dll")]
Public static extern bool sendmessage (intptr hwnd, int wmsg, bool wparam, int lparam );
[Dllimport ("user32.dll")]
Public static extern bool sendmessage (intptr hwnd, int wmsg, short wparam, int lparam );
[Dllimport ("user32.dll")]
Public static extern bool sendmessage (intptr hwnd, int wmsg, short wparam, frameeventhandler lparam );
[Dllimport ("user32.dll")]
Public static extern bool sendmessage (intptr hwnd, int wmsg, int wparam, ref bitmapinfo lparam );
[Dllimport ("user32.dll")]
Public static extern int setwindowpos (intptr hwnd, int hwndinsertafter, int X, int y, int CX, int cy, int wflags );
[Dllimport ("avicap32.dll")]
Public static extern int capgetvideoformat (intptr hwnd, intptr psvideoformat, int wsize );
// Constants
Public const int wm_user = 0x400;
Public const int ws_child = 0x40000000;
Public const int ws_visible = 0x10000000;
Public const int swp_nomove = 0x2;
Public const int swp_nozorder = 0x4;
Public const int wm_cap_driver_connect = wm_user + 10;
Public const int wm_cap_driver_disconnect = wm_user + 11;
Public const int wm_cap_set_callback_frame = wm_user + 5;
Public const int wm_cap_set_preview = wm_user + 50;
Public const int wm_cap_set_previewrate = wm_user + 52;
Public const int wm_cap_set_videoformat = wm_user + 45;
// Structures
[Structlayout (layoutkind. Sequential)]
Public struct videohdr
{
[Financialas (unmanagedtype. I4)]
Public int lpdata;
[Financialas (unmanagedtype. I4)]
Public int dwbufferlength;
[Financialas (unmanagedtype. I4)]
Public int dwbytesused;
[Financialas (unmanagedtype. I4)]
Public int dwtimecaptured;
[Financialas (unmanagedtype. I4)]
Public int dwuser;
[Financialas (unmanagedtype. I4)]
Public int dwflags;
[Financialas (unmanagedtype. byvalarray, sizeconst = 4)]
Public int [] dwreserved;
}
[Structlayout (layoutkind. Sequential)]
Public struct bitmapinfoheader
{
[Financialas (unmanagedtype. I4)]
Public int32 bisize;
[Financialas (unmanagedtype. I4)]
Public int32 biwidth;
[Financialas (unmanagedtype. I4)]
Public int32 biheight;
[Financialas (unmanagedtype. I2)]
Public short biplanes;
[Financialas (unmanagedtype. I2)]
Public short bibitcount;
[Financialas (unmanagedtype. I4)]
Public int32 bicompression;
[Financialas (unmanagedtype. I4)]
Public int32 bisizeimage;
[Financialas (unmanagedtype. I4)]
Public int32 bixpelspermeter;
[Financialas (unmanagedtype. I4)]
Public int32 biypelspermeter;
[Financialas (unmanagedtype. I4)]
Public int32 biclrused;
[Financialas (unmanagedtype. I4)]
Public int32 biclrimportant;
}
[Structlayout (layoutkind. Sequential)]
Public struct bitmapinfo
{
[Financialas (unmanagedtype. struct, sizeconst = 40)]
Public bitmapinfoheader bmiheader;
[Financialas (unmanagedtype. byvalarray, sizeconst = 1024)]
Public int32 [] bmicolors;
}
Public Delegate void frameeventhandler (intptr lwnd, intptr lpvhdr );
// Public methods
Public static object getstructure (intptr, valuetype structure)
{
Return marshal. ptrtostructure (PTR, structure. GetType ());
}
Public static object getstructure (int ptr, valuetype structure)
{
Return getstructure (New intptr (PTR), structure );
}
Public static void copy (intptr, byte [] data)
{
Marshal. Copy (PTR, Data, 0, Data. Length );
}
Public static void copy (int ptr, byte [] data)
{
Copy (New intptr (PTR), data );
}
Public static int sizeof (object structure)
{
Return marshal. sizeof (structure );
}
}
// web camera class
public class webcamera
{< br> // constructur
Public webcamera (intptr handle, int width, int height)
{< br> mcontrolptr = handle;
mwidth = width;
mheight = height;
}< br> // delegate for frame callback
Public Delegate void recievedframeeventhandler (byte [] data);
public event recievedframeeventhandler recievedframe;
Private intptr lwndc; // holds the unmanaged handle of the control
Private intptr mcontrolptr; // holds the managed pointer of the control
Private int mwidth;
Private int mheight;
Private showvideo. frameeventhandler mframeeventhandler; // delegate instance for the frame callback-must keep alive! GC shoshould not collect it
// Close the web camera
Public void closewebcam ()
{
This. capdriverdisconnect (this. lwndc );
}
// Start the web camera
Public void startwebcam ()
{
Byte [] lpszname = new byte [1, 100];
Byte [] lpszver = new byte [1, 100];
Showvideo. capgetdriverdescriptiona (0, lpszname, 100, lpszver, 100 );
This. lwndc = showvideo. capcreatecapturew.wa (lpszname, showvideo. ws_visible + showvideo. ws_child, 0, 0, mwidth, mheight, mcontrolptr, 0 );
If (this. capdriverconnect (this. lwndc, 0 ))
{
This. cappreviewrate (this. lwndc, 66 );
This. cappreview (this. lwndc, true );
Showvideo. bitmapinfo = new showvideo. bitmapinfo ();
Bitmapinfo. bmiheader. bisize = showvideo. sizeof (bitmapinfo. bmiheader );
Bitmapinfo. bmiheader. biwidth = 352;
Bitmapinfo. bmiheader. biheight = 288;
Bitmapinfo. bmiheader. biplanes = 1;
Bitmapinfo. bmiheader. bibitcount = 24;
This. capsetvideoformat (this. lwndc, ref bitmapinfo, showvideo. sizeof (bitmapinfo ));
This. mframeeventhandler = new showvideo. frameeventhandler (framecallback );
This. capsetcallbackonframe (this. lwndc, this. mframeeventhandler );
Showvideo. setwindowpos (this. lwndc, 0, 0, 0, mwidth, mheight, 6 );
}
}
// Private Functions
Private bool capdriverconnect (intptr lwnd, short I)
{
Return showvideo. sendmessage (lwnd, showvideo. wm_cap_driver_connect, I, 0 );
}
Private bool capdriverdisconnect (intptr lwnd)
{
Return showvideo. sendmessage (lwnd, showvideo. wm_cap_driver_disconnect, 0, 0 );
}
Private bool cappreview (intptr lwnd, bool F)
{Return showvideo. sendmessage (lwnd, showvideo. wm_cap_set_preview, F, 0 );}
Private bool cappreviewrate (intptr lwnd, short WMS)
{
Return showvideo. sendmessage (lwnd, showvideo. wm_cap_set_previewrate, WMS, 0 );
}
Private bool capsetcallbackonframe (intptr lwnd, showvideo. frameeventhandler lpproc)
{
Return showvideo. sendmessage (lwnd, showvideo. wm_cap_set_callback_frame, 0, lpproc );
}
Public bool capsetvideoformat (intptr hcapwnd, ref showvideo. bitmapinfo BMP format, int capformatsize)
{
Return showvideo. sendmessage (hcapwnd, showvideo. wm_cap_set_videoformat, capformatsize, ref BMP format );
}
Public void framecallback (intptr lwnd, intptr lpvhdr)
{
Showvideo. videohdr videoheader = new showvideo. videohdr ();
Byte [] videodata;
Videoheader = (showvideo. videohdr) showvideo. getstructure (lpvhdr, videoheader );
Videodata = new byte [videoheader. dwbytesused];
Showvideo. Copy (videoheader. lpdata, videodata );
If (this. recievedframe! = NULL)
This. recievedframe (videodata );
}
}
}
call
WC = new webcam in Program . webcamera (panel1.handle, panel1.width, panel1.height);
WC. startwebcam ();