C#实现控制摄像头的类

2019-12-26 13:22:03王冬梅
  • /// <summary>  /// 初始化摄像头 
  • /// </summary>  /// <param name="handle">控件的句柄</param> 
  • /// <param name="left">开始显示的左边距</param>  /// <param name="top">开始显示的上边距</param> 
  • /// <param name="width">要显示的宽度</param>  /// <param name="height">要显示的长度</param> 
  • public Pick(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 capCreateCaptureWindowA(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>  /// 开始显示图像 
  • /// </summary>  public void Start() 
  • {  if (bStat) 
  • return;  bStat = true; 
  • byte[] lpszName = new byte[100];  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, 66, 0); 
  • SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);  SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);