C # camera operations
It is a great honor to share the same information.
This is a common method for the masses. As some netizens and friends ask the same question, I will post the key part of my poor code here to help more friends with many shortcomings, I can learn and communicate with you.
Go straight to the code
Private const int WS_CHILD = 0x40000000; // 1073741824
Private const int ws_visvisible = 0x10000000; // 268435456
Private const int WM_USER = 0x400; // 1024
Private const int WM_CAP_START = WM_USER; // 1024
Private const int WM_CAP_STOP = WM_CAP_START + 68; // 1092
Private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10; // 1034
Private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11; // 1035
Private const int WM_CAP_SAVEDIB = WM_CAP_START + 25; // 1049
Private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60; // 1084
Private const int WM_CAP_SEQUENCE = WM_CAP_START + 62; // 1086
Private const int WM_CAP_FILE_SET_CAPTRUE_FILEA = WM_CAP_START + 20; // 1044
Private const int WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63; // 1087
Private const int WM_CAP_CAPTURE_IMAGE_CLIPBOARD = WM_CAP_START + 30; // 1054 copy the captured image to the clipboard
Private const int WM_CAP_SET_OVERLAY = WM_CAP_START + 51; // 1075
Private const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50; // 1074
Private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6; // 1030
Private const int WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2; // 1026
Private const int WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3; // 1027
Private const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5; // 1029
Private const int WM_CAP_SET_SCALE = WM_CAP_START + 53; // 1077
Private const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52; // 1076
Private const int WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42; // 1066 select the camera
///
/// Enable the video camera
///
///
Public static void Start ()
{
If (bWorkStart)
{
Return;
}
Byte [] lpszName = new byte [1, 100];
Try
{
HWndC = Windows. capCreateCaptureWindow (lpszName, WS_CHILD | WS_VISIBLE, mLeft, mTop, mWidth, mHeight, mControlPtr, 0 );
If (hWndC. ToInt32 ()! = 0)
{
Windows. SendMessage (hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0); // sets the returned video stream.
Windows. SendMessage (hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0); // sets the returned error
Windows. SendMessage (hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0); // set the return status
// ==================================== This article is relatively slow ==================== ==========
Windows. SendMessage (hWndC, WM_CAP_DRIVER_CONNECT, 0, 0); // device connection
// ================================================ ====================
// Windows. SendMessage (hWndC, WM_CAP_SEQUENCE, 0, 0); // start capturing
// ================================================ ====================
Windows. SendMessage (hWndC, WM_CAP_SET_SCALE, 1, 0); // set the ratio
Windows. SendMessage (hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0); // you can specify the preview speed.
Windows. SendMessage (hWndC, WM_CAP_SET_OVERLAY, 1, 0); // sets the overlay.
Windows. SendMessage (hWndC, WM_CAP_SET_PREVIEW, 1, 0); // sets Preview
BWorkStart = true;
}
// Return true;
}
Catch (Exception)
{
// Return false;
}
// Finally
//{
// Write the log Global. log. Write ("SendMessage");
// LogClass. WriteLog ("SendMessage ");
//}
}
///
/// Disable the video camera
///
Public static void Stop ()
{
Windows. SendMessage (hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0 );
BWorkStart = false;
}
///
/// Capture or take a photo and save it to the specified path
///
/// Path of the bmp file to be saved
Public static void GrabImage (string path)
{
IntPtr hBmp = Marshal. StringToHGlobalAnsi (path );
Windows. SendMessage (hWndC, WM_CAP_SAVEDIB, 0, hBmp. ToInt32 (); // 0x41e
}
///
/// Copy the captured image to the clipboard
///
///
Public static bool CopyToClipBoard ()
{
Return Windows. SendMessage (hWndC, WM_CAP_CAPTURE_IMAGE_CLIPBOARD, 0, 0 );
}