Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. runtime. interopservices;
Namespace windowsformsapplication24
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Const short wm_cap = 1024;
Const int wm_cap_driver_connect = wm_cap + 10;
Const int wm_cap_driver_disconnect = wm_cap + 11;
Const int wm_cap_edit_copy = wm_cap + 30;
Const int wm_cap_set_preview = wm_cap + 50;
Const int wm_cap_set_previewrate = wm_cap + 52;
Const int wm_cap_set_scale = wm_cap + 53;
Const int ws_child = 1073741824;
Const int ws_visible = 268435456;
Const short swp_nomove = 2;
Const short swp_nosize = 1;
Const short swp_nozorder = 4;
Const short hwnd_bottom = 1;
Int idevice = 0;
Int hhwnd;
[System. runtime. interopservices. dllimport ("USER32", entrypoint = "sendmessagea")]
Static extern int sendmessage (INT hwnd, int wmsg, int wparam, [financialas (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 );
[System. runtime. interopservices. dllimport ("avicap32.dll")]
Static extern int capcreatecapturew.wa (string lpszwindowname, int dwstyle, int X, int y, int nwidth, short nheight, int hwndparent, int NID );
[System. runtime. interopservices. dllimport ("avicap32.dll")]
Static extern bool capgetdriverdescriptiona (short wdriver, string lpszname, int cbname, string lpszver, int cbver );
Private void openpreviewwindow ()
{
Int iheight = 320;
Int iwidth = 200;
//
// Open preview window in picturebox
//
Hhwnd = capcreatecapturew.wa (idevice. tostring (), (ws_visible | ws_child), 0, 0,640,480, piccapture. Handle. toint32 (), 0 );
//
// Connect to Device
//
If (sendmessage (hhwnd, wm_cap_driver_connect, idevice, 0) = 1)
{
//
// Set the preview Scale
//
Sendmessage (hhwnd, wm_cap_set_scale, 1, 0 );
//
// Set the preview rate in milliseconds
//
Sendmessage (hhwnd, wm_cap_set_previewrate, 66, 0 );
//
// Start previewing the image from the camera
//
Sendmessage (hhwnd, wm_cap_set_preview, 1, 0 );
//
// Resize window to fit in picturebox
//
Setwindowpos (hhwnd, hwnd_bottom, 0, 0, iwidth, iheight, (swp_nomove | swp_nozorder ));
}
Else
{
//
// Error connecting to device close window
//
Destroywindow (hhwnd );
}
}
Private void closepreviewwindow ()
{
//
// Disconnect from Device
//
Sendmessage (hhwnd, wm_cap_driver_disconnect, idevice, 0 );
//
// Close Window
//
Destroywindow (hhwnd );
}
Private void button#click (Object sender, eventargs E)
{
Idevice = int. parse (device_number_textbox.text );
Openpreviewwindow ();
}
}
}
Piccapture is a picturebox
Device_number_textbox.text = 0 is a textbox with a value of 0.
by hhc123