c# - How do I capture video from a webcam? -
i need capture video webcam. there classes in c#/.net can me this. interested in real time data.
and there c#/.net books can study gain deep knowledge on language , platform?
this use. need first class iterate devices:
public class devicemanager { [dllimport("avicap32.dll")] protected static extern bool capgetdriverdescriptiona(short wdriverindex, [marshalas(unmanagedtype.vbbyrefstr)]ref string lpszname, int cbname, [marshalas(unmanagedtype.vbbyrefstr)] ref string lpszver, int cbver); static arraylist devices = new arraylist(); public static tcamdevice[] getalldevices() { string dname = "".padright(100); string dversion = "".padright(100); (short = 0; < 10; i++) { if (capgetdriverdescriptiona(i, ref dname, 100, ref dversion, 100)) { tcamdevice d = new tcamdevice(i); d.name = dname.trim(); d.version = dversion.trim(); devices.add(d); } } return (tcamdevice[])devices.toarray(typeof(tcamdevice)); } public static tcamdevice getdevice(int deviceindex) { return (tcamdevice)devices[deviceindex]; } }
and 1 control cam.
public class tcamdevice { private const short wm_cap = 0x400; private const int wm_cap_driver_connect = 0x40a; private const int wm_cap_driver_disconnect = 0x40b; private const int wm_cap_edit_copy = 0x41e; private const int wm_cap_set_preview = 0x432; private const int wm_cap_set_overlay = 0x433; private const int wm_cap_set_previewrate = 0x434; private const int wm_cap_set_scale = 0x435; private const int ws_child = 0x40000000; private const int ws_visible = 0x10000000; [dllimport("avicap32.dll")] protected static extern int capcreatecapturewindowa([marshalas(unmanagedtype.vbbyrefstr)] ref string lpszwindowname, int dwstyle, int x, int y, int nwidth, int nheight, int hwndparent, int nid); [dllimport("user32", entrypoint = "sendmessagea")] protected static extern int sendmessage(int hwnd, int wmsg, int wparam, [marshalas(unmanagedtype.asany)] object lparam); [dllimport("user32")] protected static extern int setwindowpos(int hwnd, int hwndinsertafter, int x, int y, int cx, int cy, int wflags); [dllimport("user32")] protected static extern bool destroywindow(int hwnd); int index; int devicehandle; public tcamdevice(int index) { this.index = index; } private string _name; public string name { { return _name; } set { _name = value; } } private string _version; public string version { { return _version; } set { _version = value; } } public override string tostring() { return this.name; } /// <summary> /// initialize device /// </summary> /// <param name="windowheight">height of window</param> /// <param name="windowwidth">width of window</param> /// <param name="handle">the control handle attach device</param> public void init(int windowheight, int windowwidth, int handle) { string deviceindex = convert.tostring(this.index); devicehandle = capcreatecapturewindowa(ref deviceindex, ws_visible | ws_child, 0, 0, windowwidth, windowheight, handle, 0); if (sendmessage(devicehandle, wm_cap_driver_connect, this.index, 0) > 0) { sendmessage(devicehandle, wm_cap_set_scale, -1, 0); sendmessage(devicehandle, wm_cap_set_previewrate, 0x42, 0); sendmessage(devicehandle, wm_cap_set_preview, -1, 0); setwindowpos(devicehandle, 1, 0, 0, windowwidth, windowheight, 6); } } /// <summary> /// shows webcam preview in control /// </summary> /// <param name="windowscontrol">control attach webcam preview</param> public void showwindow(global::system.windows.forms.control windowscontrol) { init(windowscontrol.height, windowscontrol.width, windowscontrol.handle.toint32()); } /// <summary> /// stop webcam , destroy handle /// </summary> public void stop() { sendmessage(devicehandle, wm_cap_driver_disconnect, this.index, 0); destroywindow(devicehandle); } }
the showwindow takes picturebox parameter.
Comments
Post a Comment