C # Call the camera

Source: Internet
Author: User
Using system;

Using system. Collections. Generic;

Using system. text;

Using system. runtime. interopservices;

Public class videowork

{

Private const int wm_user = 0 ×400;

Private const int ws_child = 0 ×40000000;

Private const int ws_visible = 0 × 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 </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 );

//

// Note that because long in winapi is 32-bit, and long in C # Is 64wei, you need to set lparam to int.

//

[Dllimport ("user32.dll")]

Private Static extern bool sendmessage (intptr hwnd, int wmsg, int wparam, int lparam );

/// <Summary>

/// Start displaying the image

/// </Summary>

Public void start ()

{

If (bworkstart)

Return;

Bworkstart = true;

Byte [] lpszname = new byte [1, 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 the display

/// </Summary>

Public void stop ()

{

Sendmessage (hwndc, wm_cap_driver_disconnect, 0, 0 );

Bworkstart = false;

}

/// <Summary>

/// Capture

/// </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. toint32 ());

}

}

Call method:

Drag a panel into the form.

The call code is:

Videowork WV = new videowork (panel1.handle, 0, 0, panel1.width, panel1.height );

WV. Start ();

Related Article

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.