C # camera screenshot

Source: Internet
Author: User
Using  System;  Using  System. runtime. interopservices;  Using  System. drawing;  Using  System. Drawing. imaging;  Namespace  Video {  ///   <Summary>  ///  A class for controlling cameras  ///   </Summary> Public   Class  Videowork {  Private   Const   Int Wm_user = Zero X 400  ;  Private   Const   Int Ws_child = Zero X 40000000  ;  Private   Const   Int Ws_visible =Zero X 10000000  ;  Private   Const   Int Wm_cap_start = Wm_user;  Private   Const   Int Wm_cap_stop = wm_cap_start + 68  ;  Private   Const   Int Wm_cap_driver_connect = wm_cap_start + 10 ;  Private   Const   Int Wm_cap_driver_disconnect = wm_cap_start + 11  ;  Private   Const   Int Wm_cap_savedib = wm_cap_start + 25  ;  Private   Const   Int Wm_cap_grab_frame = wm_cap_start + 60 ;  Private   Const   Int Wm_cap_sequence = wm_cap_start + 62  ;  Private   Const   Int Wm_cap_file_set_capture_filea = wm_cap_start + 20  ;  Private   Const   Int Wm_cap_sequence_nofile = wm_cap_start + 63 ;  Private   Const   Int Wm_cap_set_overlay = wm_cap_start + 51  ;  Private   Const   Int Wm_cap_set_preview = wm_cap_start + 50  ;  Private   Const   Int Wm_cap_set_callback_videostream = wm_cap_start + 6 ;  Private   Const   Int Wm_cap_set_callback_error = wm_cap_start + 2  ;  Private   Const   Int Wm_cap_set_callback_statusa = wm_cap_start + 3  ;  Private   Const   Int Wm_cap_set_callback_frame = wm_cap_start + 5 ;  Private   Const   Int Wm_cap_set_scale = wm_cap_start + 53  ;  Private   Const   Int Wm_cap_set_previewrate = wm_cap_start + 52  ;  Private  Intptr hwndc;  Private   Bool Bworkstart =False  ;  Private  Intptr mcontrolptr;  Private   Int  Mwidth;  Private   Int  Mheight;  Private   Int  Mleft;  Private   Int  MTop;  ///  <Summary>  ///  Initialize the display image  ///   </Summary>  ///   <Param name = "handle">  Control handle  </Param>  ///   <Param name = "Left">  Left margin of start display  </Param>  ///   <Param name = "TOP">  Top margin of start display </Param>  ///   <Param name = "width">  Width to be displayed  </Param>  ///   <Param name = "height">  Length to be displayed  </Param>  Public Videowork (intptr handle, Int Left, Int Top, Int Width, Int Height) {mcontrolptr = Handle; mwidth = Width; mheight = Height; mleft = Left; MTop = Top;} [dllimport (  "  Avicap32.dll  "  )]  Private   Static   Extern Intptr capcreatecapturew.wa ( Byte [] Lpszwindowname, Int Dwstyle, Int X, Int Y, Int Nwidth, Int Nheight, intptr hwndparent, Int  NID); [dllimport (  "  Avicap32.dll  "  )]  Private   Static   Extern  Int Capgetvideoformat (intptr hwnd, intptr psvideoformat, Int  Wsize); [dllimport (  "  User32.dll  "  )]  Private   Static   Extern   Bool Sendmessage (intptr hwnd, Int Wmsg, Int Wparam, Long Lparam );  ///   <Summary>  ///  Start displaying images  ///   </Summary>  Public   Void  Start (){  If  (Bworkstart)  Return  ; Bworkstart = True  ; Byte [] Lpszname = New   Byte [ 100  ]; Hwndc = Capcreatecapturew.wa (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,  66 , 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   Void  Stop () {sendmessage (hwndc, wm_cap_driver_disconnect,  0 , 0  ); Bworkstart = False  ;}  ///   <Summary>  ///  Capture images  ///  </Summary>  ///   <Param name = "path">  Path of the BMP file to be saved  </Param>  Public   Void Grabimage ( String  Path) {intptr hbmp = Marshal. stringtohglobalansi (PATH); sendmessage (hwndc, wm_cap_savedib,  0  , Hbmp. toint64 ());}}} 

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.