如有雷同,不勝榮幸,若轉載,請注明
這個是大眾普通方法,鑒於有網友和朋友問同樣的問題,在這裡將我的拙劣的代碼關鍵部分貼出來,以便幫助更多的朋友們,不足之處甚多,我可以學習,交流,請教閣下
廢話到此,直接上代碼
private const int WS_CHILD = 0x40000000; //1073741824
///
/// 開啟視頻攝像頭
///
///
public static void Start()
{
if (bWorkStart)
{
return;
}
byte[] lpszName = new byte[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); //設置返回視頻流
Windows.SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0); //設置返回錯誤
Windows.SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0); //設置返回狀態
//=====================這條比較慢=======================
Windows.SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0); //設備連接
//=====================================================
//Windows.SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0); //開始捕捉
//=====================================================
Windows.SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0); //設置比例
Windows.SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0); //設置預覽速度
Windows.SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0); //設置疊加
Windows.SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0); //設置預覽
bWorkStart = true;
}
//return true;
}
catch (Exception)
{
//return false;
}
//finally
//{
//寫日志 Global.log.Write( “SendMessage “);
//LogClass.WriteLog("SendMessage");
//}
}
///
/// 關閉視頻攝像頭
///
public static void Stop()
{
Windows.SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
bWorkStart = false;
}
///
/// 抓圖或者拍照,然後保存到指定路徑下
///
/// 要保存bmp文件的路徑
public static void GrabImage(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
Windows.SendMessage(hWndC, WM_CAP_SAVEDIB, 0, hBmp.ToInt32()); //0x41e
}
///
/// 捕捉圖像復制到剪切板
///
///
public static bool CopyToClipBoard()
{
return Windows.SendMessage(hWndC, WM_CAP_CAPTURE_IMAGE_CLIPBOARD, 0, 0);
}