WinForm Program Call Camera

Source: Internet
Author: User

The following code can be used directly!

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices; Public classvideowork{Private Const intWm_user =0x400; Private Const intWs_child =0x40000000; Private Const intWs_visible =0x10000000; Private Const intWm_cap_start =Wm_user; Private Const intWm_cap_stop = Wm_cap_start + the; Private Const intWm_cap_driver_connect = Wm_cap_start +Ten; Private Const intWm_cap_driver_disconnect = Wm_cap_start + One; Private Const intWm_cap_savedib = Wm_cap_start + -; Private Const intWm_cap_grab_frame = Wm_cap_start + -; Private Const intWm_cap_sequence = Wm_cap_start + +; Private Const intWm_cap_file_set_capture_filea = Wm_cap_start + -; Private Const intWm_cap_sequence_nofile = Wm_cap_start + the; Private Const intWm_cap_set_overlay = Wm_cap_start +Wuyi; Private Const intWm_cap_set_preview = Wm_cap_start + -; Private Const intWm_cap_set_callback_videostream = Wm_cap_start +6; Private Const intWm_cap_set_callback_error = Wm_cap_start +2; Private Const intWm_cap_set_callback_statusa = Wm_cap_start +3; Private Const intWm_cap_set_callback_frame = Wm_cap_start +5; Private Const intWm_cap_set_scale = Wm_cap_start + -; Private Const intWm_cap_set_previewrate = Wm_cap_start + the; PrivateIntPtr HWNDC; Private BOOLBworkstart =false; PrivateIntPtr mcontrolptr; Private intMwidth; Private intMheight; Private intMleft; Private intMtop; /**/    ///   <summary>     ///initializing the display image///   </summary>     ///   <param name= "Handle" >handle to the control</param>     ///   <param name= "left" >left margin to start display</param>     ///   <param name= "Top" >top margin to start showing</param>     ///   <param name= "width" >the width to display</param>     ///   <param name= "height" >the length to display</param>      PublicVideowork (INTPTR handle,intLeftintTopintWidthintheight) {Mcontrolptr=handle; Mwidth=width; Mheight=height; Mleft=Left ; Mtop=top; } [DllImport ("Avicap32.dll")]    Private Static externIntPtr Capcreatecapturewindowa (byte[] lpszWindowName,intDwstyle,intXintYintNwidth,intNheight, IntPtr hwndparent,intNID); [DllImport ("Avicap32.dll")]    Private Static extern intCapgetvideoformat (IntPtr hWnd, IntPtr Psvideoformat,intwsize); //     //It is particularly important to note that because long in WINAPI is 32 bits and long in C # is 64wei, you need to lparam the int//[DllImport ("User32.dll")]    Private Static extern BOOLSendMessage (IntPtr hWnd,intWmsg,intWParam,intLParam); /**/    ///   <summary>     ///Start Display Image///   </summary>      Public voidStart () {if(Bworkstart)return; Bworkstart=true; byte[] LpszName =New byte[ -]; HWNDC= Capcreatecapturewindowa (LpszName, Ws_child | Ws_visible, Mleft, Mtop, Mwidth, Mheight, Mcontrolptr,0); if(Hwndc.toint32 ()! =0) {SendMessage (HWNDC, Wm_cap_set_callback_videostream,0,0); SendMessage (HWNDC, Wm_cap_set_callback_error,0,0); SendMessage (HWNDC, Wm_cap_set_callback_statusa,0,0); SendMessage (HWNDC, Wm_cap_driver_connect,0,0); SendMessage (HWNDC, Wm_cap_set_scale,1,0); SendMessage (HWNDC, Wm_cap_set_previewrate, the,0); SendMessage (HWNDC, Wm_cap_set_overlay,1,0); SendMessage (HWNDC, Wm_cap_set_preview,1,0); //Global.log.Write ("SendMessage");        }        return; }    /**/    ///   <summary>     ///Stop Display///   </summary>      Public voidStop () {SendMessage (HWNDC, Wm_cap_driver_disconnect,0,0); Bworkstart=false; }    /**/    ///   <summary>     ///Grab Map///   </summary>     ///   <param name= "path" >the path to save the BMP file</param>      Public voidGrabimage (stringpath) {INTPTR hbmp=marshal.stringtohglobalansi (path); SendMessage (HWNDC, Wm_cap_savedib,0, Hbmp.toint32 ()); }} Call Method: Drag a panel call code on the form: Videowork WV=NewVideowork (Panel1. Handle,0,0, Panel1. Width, Panel1. Height); WV. Start ();

The above code, can be implemented and uploaded, according to the need to call different methods!

WinForm Program Call Camera

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.