相信使用 C#、winforms 编写界面的朋友都遇到过在程序调用摄像头时弹出“选择摄像头源”并且摄像头展示 panel “黑屏”的情况,网上关于此类问题有相当多的提问,但回答却大多不尽人意,正好最近在写毕设时也踩到了这个坑上,研究出了一个解决办法,虽不能非常完美的解决问题,但至少能保证每次都成功启动摄像头

一、检查 .net 的版本

在网上流传的 C# 调用摄像头的案例中,大多都是使用了 avicap32.dll 与 user32.dll 等比较老的动态链接库,此类库对程序运行时的环境有要求,经测试后发现,通过这俩类库调用摄像头的代码 .net v4.0 及后的版本的环境中基本无法运行。因此,若使用了 avicap32.dll 与 user32.dll 类库调用摄像头,请确保程序运行在 .net v3.0 或更老的版本下

二、创建摄像头类 WebCamera.cs

  • 系统环境:Win10 x64
  • VS版本:Visual Studio 2015
  • 运行环境:.net framework 3.0
using System;
using System.Runtime.InteropServices;

namespace WebcamTest
{
  class WebCam
  {
    private const int WM_USER = 0x400;
    private const int WS_CHILD = 0x40000000;
    private const int WS_VISIBLE = 0x10000000;
    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 const int WM_CAP_EDIT_COPY = WM_USER + 30;
    private IntPtr hWndC;
    private bool bStat = false;
    private IntPtr mControlPtr;
    private int mWidth;
    private int mHeight;
    private int mLeft;
    private int mTop;
///
/// 初始化摄像头
///
/// 控件的句柄
/// 开始显示的左边距
/// 开始显示的上边距
/// 要显示的宽度
/// 要显示的长度
    public WebCam(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, int lParam);
///
/// 开始显示图像
///
    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);

//大力出奇迹!
        while (!SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0))
        {
          continue;
        }

        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);
      }
      return;
    }
///
/// 停止显示
///
    public void Stop()
    {
      SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
      bStat = false;
    }
///
/// 截图并复制到粘贴板
///
    public void copyToClipBoard()
    {
      SendMessage(hWndC, WM_CAP_EDIT_COPY, 0, 0);
    }

  }
}

解决办法在于://大力出奇迹!的注释下

while (!SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0))
{
    continue;
}

此处利用死循环调用 SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0) 去链接摄像头,直到返回调用成功,看似暴力的解决办法缺有效地解决了调用失败的方法。至于此方法的灵感来源,来自于反复折腾代码无果时生气地连续点击“开启摄像头”后出现的偶然成功:p

三、一个简单的调用例子 Mainform.cs

编写完摄像头类 WebCamera.cs 后,就可以新建一个窗体测试了

using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace WebcamTest
{
    public partial class MainForm : Form
    {

        WebCam webcam;
        Bitmap bitmap; //存放摄像头截取图片

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //程序载入时自动开启摄像头
            webcam = new WebCam(panel_WebCamShower.Handle, 0, 0, panel_WebCamShower.Width, panel_WebCamShower.Height);
            webcam.Start();
        }

        private void btn_StartCam_Click(object sender, EventArgs e)
        {
            //开启摄像头
            webcam.Start();
        }

        private void btn_StopCam_Click(object sender, EventArgs e)
        {
            //关闭摄像头
            webcam.Stop();
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            //退出程序
            Application.Exit();
        }

        private void btn_CopyToClipBoard_Click(object sender, EventArgs e)
        {
            //复制到粘贴板
            webcam.copyToClipBoard();

            //从粘贴板提取图片
            bitmap = (Bitmap)Clipboard.GetImage();

            //刷新画布
            panel_ClipBoardShower.Refresh();

        }
    }
}

四、测试

按照上述的暴力方式改造好代码后,在测试时,选择视频源的弹框一般还会弹出一到两次,在按一到两次确定后摄像头便调用成功