After installing the camera program, you can find an avicap32.dll file.

Source: Internet
Author: User
Tags keep alive

Using system;
Using system. runtime. interopservices;

Namespace webcam
{
///
/// Summary of avicap.
///
Public class showvideo
{
// Showvideo CILS
[Dllimport ("avicap32.dll")] public static extern intptr capcreatecapture0000wa (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
{
// Constructur
Public webcamera (intptr handle, int width, int height)
{
Mcontrolptr = handle;
Mwidth = width;
Mheight = height;
}

// 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 );
}

Private bool capsetvideoformat (intptr hcapwnd, ref showvideo. bitmapinfo BMP format, int capformatsize)
{
Return showvideo. sendmessage (hcapwnd, showvideo. wm_cap_set_videoformat, capformatsize, ref BMP format );
}

Private 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 );
}
}

}

The specific call is as follows:

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using webcam;

Namespace webcam
{
///
/// Summary of form1.
///
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. Panel panelpreview;
Private system. Windows. Forms. Button B _play;
Private system. Windows. Forms. Button B _stop;
///
/// Required designer variables.
///
Private system. componentmodel. Container components = NULL;
Webcamera WC;

Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

//
// Todo: add Any constructor code after initializecomponent calls
//
}

///
/// Clear all resources in use.
///
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
///
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
///
Private void initializecomponent ()
{
This. B _play = new system. Windows. Forms. Button ();
This. panelpreview = new system. Windows. Forms. Panel ();
This. B _stop = new system. Windows. Forms. Button ();
This. suspendlayout ();
//
// B _play
//
This. B _play.location = new system. Drawing. Point (280,368 );
This. B _play.name = "B _play ";
This. B _play.tabindex = 0;
This. B _play.text = "& Play ";
This. B _play.click + = new system. eventhandler (this. button#click );
//
// Panelpreview
//
This. panelpreview. Location = new system. Drawing. Point (8, 8 );
This. panelpreview. Name = "panelpreview ";
This. panelpreview. size = new system. Drawing. Size (344,272 );
This. panelpreview. tabindex = 1;
//
// B _stop
//
This. B _stop.enabled = false;
This. B _stop.location = new system. Drawing. Point (360,368 );
This. B _stop.name = "B _stop ";
This. B _stop.tabindex = 2;
This. B _stop.text = "& stop ";
This. B _stop.click + = new system. eventhandler (this. B _stop_click );
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (464,413 );
This. Controls. Add (this. B _stop );
This. Controls. Add (this. panelpreview );
This. Controls. Add (this. B _play );
This. maximizebox = false;
This. minimizebox = false;
This. Name = "form1 ";
This. Text = "goodview test web camera ";
This. Load + = new system. eventhandler (this. form#load );
This. resumelayout (false );

}
# Endregion

///
/// Main entry point of the application.
///
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}

Private void form1_load (Object sender, system. eventargs E)
{
B _play.enabled = false;
B _stop.enabled = true;
Panelpreview. size = new size (330,330 );
WC = new webcamera (panelpreview. Handle, panelpreview. Width, panelpreview. Height );
WC. startwebcam ();
}

Private void button#click (Object sender, system. eventargs E)
{
B _play.enabled = false;
B _stop.enabled = true;
Panelpreview. size = new size (330,330 );
WC = new webcamera (panelpreview. Handle, panelpreview. Width, panelpreview. Height );
WC. startwebcam ();
}

Private void B _stop_click (Object sender, system. eventargs E)
{
B _play.enabled = true;
B _stop.enabled = false;
WC. closewebcam ();
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.