---
title: "c# 获取 摄像头 实现录像"
author: "holoblog"
date: "2011-11-06T13:23:00Z"
tags: []
slug: "c"
draft: false
lastmod: "2011-11-20T20:25:18Z"
id: 1268912
url: "https://holoblog.iteye.com/blog/1268912"
---

c# 获取 摄像头 实现录像

利用普通的简易摄像头，通过C#语言即可开发成简易视频程序。本实例利用市场上购买的普通摄像头，利用VFW技术，实现单路视频监控系统。运行程序，窗体中将显示舰体摄像头采集的视频信息。如图13.9所示。  
技术要点  
本实例主要使用了VFW（Video for Windows）技术。VFW 是Microsoft公司为开发Windows平台下的视频应用程序提供的软件工具包，提供了一系列应用程序编程接口（API），用户可以通过这些接口很方便地实现视频捕获、视频编辑及视频播放等通用功能，还可利用回调函数开发比较复杂的视频应用程序。该技术的特点是播放视频时不需要专用的硬件设备，而且应用灵活，可以满足视频应用程序开发的需要。Windows操作系统自身就携带了VFW技术，系统安装时，会自动安装VFW的相关组件。  
VFW技术主要由六个功能模块组成，下面进行简单说明。  
AVICAP32.DLL：包含执行视频捕获的函数，给AVI文件的I/O处理和视频，音频设备驱动程序提供一个高级接口。  
MSVIDEO.DLL：包含一套特殊的DrawDib函数，用来处理程序上的视频操作。  
MCIAVI.DRV：包括对VFW的MCI命令解释器的驱动程序。  
AVIFILE.DLL：包含由标准多媒体I/O（mmio）函数提供的更高级的命令，用来访问.AVI文件。  
ICM：压缩管理器，用于管理的视频压缩/解压缩的编译码器。  
ACM：音频压缩管理器，提供与ICM相似的服务，适用于波形音频。  
AVICAP32.DLL中的函数和USER32.DLL中的函数，函数语法及结构如下。  
（1）capCreateCaptureWindow函数  
该函数用于创建一个视频捕捉窗口。语法如下：  
\[DllImport("avicap32.dll")]  
public static extern IntPtr capCreateCaptureWindowA(byte\[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);  
参数说明如下。  
lpszWindowName：标识窗口的名称。  
dwStyle：标识窗口风格。  
x、y：标识窗口的左上角坐标。  
nWidth、nHeight：标识窗口的宽度和高度。  
hWnd：标识父窗口句柄。  
nID：标识窗口ID。  
返回值：视频捕捉窗口句柄。  
（2）SendMessage函数  
用于向Windows系统发送消息机制。  
\[DllImport("User32.dll")]  
private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);  
参数说明如下。  
hWnd：窗口句柄。  
wMsg：将要发送的消息。  
wParam、lParam：消息的参数，每个消息都有两个参数，参数设置由发送的消息而定。

Form1窗体中通过调用视频类中的方法来实现相应的功能。

在【开始录像】按钮的Click事件中添加如下代码：

private void buttonStart\_Click(object sender, EventArgs e)  
{  
buttonStart.Enabled = false;  
buttonStop.Enabled = true;  
//btnPz.Enabled = true;  
video = new cVideo(picCapture.Handle, picCapture.Width, picCapture.Height);  
video.StartWebCam();  
}

在【停止录像】按钮的Click事件中添加如下代码：

private void buttonStop\_Click(object sender, EventArgs e)  
{  
buttonStart.Enabled = true;  
buttonStop.Enabled = false;  
buttonPz.Enabled = false;  
video.CloseWebcam();  
}

在【拍照】按钮的Click事件下添加如下代码：

private void buttonPz\_Click(object sender, EventArgs e)  
{  
video.GrabImage(picCapture.Handle, "d:\\\\a.bmp");  
}

完整代码如下：

1、Program.cs

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Windows.Forms;

namespace Video1  
{  
static class Program  
{  
/// &lt;summary&gt;  
/// 应用程序的主入口点。  
/// &lt;/summary&gt;  
\[STAThread]  
static void Main()  
{  
Application.EnableVisualStyles();  
Application.SetCompatibleTextRenderingDefault(false);  
Application.Run(new Form1());  
}  
}  
}

2、Form1.cs

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;

namespace Video1  
{  
public partial class Form1 : Form  
{  
cVideo video;  
public Form1()  
{  
InitializeComponent();  
}

private void buttonStart\_Click(object sender, EventArgs e)  
{  
buttonStart.Enabled = false;  
buttonStop.Enabled = true;  
//btnPz.Enabled = true;  
video = new cVideo(picCapture.Handle, picCapture.Width, picCapture.Height);  
video.StartWebCam();  
}

private void buttonStop\_Click(object sender, EventArgs e)  
{  
buttonStart.Enabled = true;  
buttonStop.Enabled = false;  
buttonPz.Enabled = false;  
video.CloseWebcam();  
}

private void buttonPz\_Click(object sender, EventArgs e)  
{  
video.GrabImage(picCapture.Handle, "d:\\\\a.bmp");  
}  
}  
}

3、Form1.Designer.cs

namespace Video1  
{  
partial class Form1  
{  
/// &lt;summary&gt;  
/// 必需的设计器变量。  
/// &lt;/summary&gt;  
private System.ComponentModel.IContainer components = null;

/// &lt;summary&gt;  
/// 清理所有正在使用的资源。  
/// &lt;/summary&gt;  
/// &lt;param&gt;如果应释放托管资源，为 true；否则为 false。&lt;/param&gt;  
protected override void Dispose(bool disposing)  
{  
if (disposing && (components != null))  
{  
components.Dispose();  
}  
base.Dispose(disposing);  
}

#region Windows 窗体设计器生成的代码

/// &lt;summary&gt;  
/// 设计器支持所需的方法 - 不要  
/// 使用代码编辑器修改此方法的内容。  
/// &lt;/summary&gt;  
private void InitializeComponent()  
{  
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));  
this.buttonStart = new System.Windows.Forms.Button();  
this.buttonStop = new System.Windows.Forms.Button();  
this.buttonPz = new System.Windows.Forms.Button();  
this.panelTop = new System.Windows.Forms.Panel();  
this.picCapture = new System.Windows.Forms.PictureBox();  
this.panelBottom = new System.Windows.Forms.Panel();  
this.panelTop.SuspendLayout();  
((System.ComponentModel.ISupportInitialize)(this.picCapture)).BeginInit();  
this.panelBottom.SuspendLayout();  
this.SuspendLayout();  
//  
// buttonStart  
//  
this.buttonStart.Location = new System.Drawing.Point(39, 3);  
this.buttonStart.Name = "buttonStart";  
this.buttonStart.Size = new System.Drawing.Size(88, 36);  
this.buttonStart.TabIndex = 1;  
this.buttonStart.Text = "开始录像";  
this.buttonStart.UseVisualStyleBackColor = true;  
this.buttonStart.Click += new System.EventHandler(this.buttonStart\_Click);  
//  
// buttonStop  
//  
this.buttonStop.Location = new System.Drawing.Point(181, 3);  
this.buttonStop.Name = "buttonStop";  
this.buttonStop.Size = new System.Drawing.Size(80, 36);  
this.buttonStop.TabIndex = 2;  
this.buttonStop.Text = "停止录像";  
this.buttonStop.UseVisualStyleBackColor = true;  
this.buttonStop.Click += new System.EventHandler(this.buttonStop\_Click);  
//  
// buttonPz  
//  
this.buttonPz.Location = new System.Drawing.Point(313, 3);  
this.buttonPz.Name = "buttonPz";  
this.buttonPz.Size = new System.Drawing.Size(77, 36);  
this.buttonPz.TabIndex = 3;  
this.buttonPz.Text = "拍 照";  
this.buttonPz.UseVisualStyleBackColor = true;  
this.buttonPz.Click += new System.EventHandler(this.buttonPz\_Click);  
//  
// panelTop  
//  
this.panelTop.AutoSize = true;  
this.panelTop.Controls.Add(this.picCapture);  
this.panelTop.Dock = System.Windows.Forms.DockStyle.Fill;  
this.panelTop.Location = new System.Drawing.Point(0, 0);  
this.panelTop.Name = "panelTop";  
this.panelTop.Size = new System.Drawing.Size(468, 372);  
this.panelTop.TabIndex = 4;  
//  
// picCapture  
//  
this.picCapture.Dock = System.Windows.Forms.DockStyle.Fill;  
this.picCapture.InitialImage = ((System.Drawing.Image)(resources.GetObject("picCapture.InitialImage")));  
this.picCapture.Location = new System.Drawing.Point(0, 0);  
this.picCapture.Name = "picCapture";  
this.picCapture.Size = new System.Drawing.Size(468, 372);  
this.picCapture.TabIndex = 0;  
this.picCapture.TabStop = false;  
//  
// panelBottom  
//  
this.panelBottom.Controls.Add(this.buttonStart);  
this.panelBottom.Controls.Add(this.buttonStop);  
this.panelBottom.Controls.Add(this.buttonPz);  
this.panelBottom.Dock = System.Windows.Forms.DockStyle.Bottom;  
this.panelBottom.Location = new System.Drawing.Point(0, 327);  
this.panelBottom.Name = "panelBottom";  
this.panelBottom.Size = new System.Drawing.Size(468, 45);  
this.panelBottom.TabIndex = 5;  
//  
// Form1  
//  
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);  
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
this.ClientSize = new System.Drawing.Size(468, 372);  
this.Controls.Add(this.panelBottom);  
this.Controls.Add(this.panelTop);  
this.Name = "Form1";  
this.Text = "Form1";  
this.panelTop.ResumeLayout(false);  
((System.ComponentModel.ISupportInitialize)(this.picCapture)).EndInit();  
this.panelBottom.ResumeLayout(false);  
this.ResumeLayout(false);  
this.PerformLayout();

}

#endregion

private System.Windows.Forms.PictureBox picCapture;  
private System.Windows.Forms.Button buttonStart;  
private System.Windows.Forms.Button buttonStop;  
private System.Windows.Forms.Button buttonPz;  
private System.Windows.Forms.Panel panelTop;  
private System.Windows.Forms.Panel panelBottom;  
}  
}

4、ClassVideoAPI.cs

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Drawing;  
using System.Runtime.InteropServices;  
using System.Threading;  
using System.Windows.Forms;  
using System.IO;

namespace Video1  
{  
public class VideoAPI //视频API类  
{  
// 视频ＡＰＩ调用  
\[DllImport("avicap32.dll")]//包含了执行视频捕获的函数，它给AVI文件I/O和视频、音频设备驱动程序提供一个高级接口  
public static extern IntPtr capCreateCaptureWindow(string lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hwndParent, int nID);  
\[DllImport("AVICAP32.dll", CharSet = CharSet.Unicode)]  
public static extern bool capGetDriverDescription(int wDriverIndex, StringBuilder lpszName, int cbName, StringBuilder lpszVer, int cbVer);  
\[DllImport("avicap32.dll")]  
public static extern IntPtr capCreateCaptureWindowA(byte\[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);  
\[DllImport("avicap32.dll")]  
public static extern bool capGetDriverDescriptionA(short wDriver, byte\[] lpszName, int cbName, byte\[] lpszVer, int cbVer);  
\[DllImport("User32.dll")]  
public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);  
\[DllImport("User32.dll")]  
public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);  
\[DllImport("User32.dll")]  
public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);  
\[DllImport("User32.dll")]  
public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, FrameEventHandler lParam);  
\[DllImport("User32.dll")]  
public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref BITMAPINFO lParam);  
\[DllImport("User32.dll")]  
public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref CAPDRIVERCAPS lParam);  
\[DllImport("User32.dll")]  
public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref CAPTUREPARMS lParam);  
\[DllImport("User32.dll")]  
public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref CAPSTATUS lParam);  
\[DllImport("User32.dll")]  
public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);  
\[DllImport("avicap32.dll")]  
public static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize);

// 常量  
// public const int WM\_USER = 0x400;  
public const int WS\_CHILD = 0x40000000;  
public const int WS\_VISIBLE = 0x10000000;

public const int SWP\_NOMOVE = 0x2;  
public const int SWP\_NOZORDER = 0x4;  
// public const int WM\_CAP\_DRIVER\_CONNECT = WM\_USER + 10;  
// public const int WM\_CAP\_DRIVER\_DISCONNECT = WM\_USER + 11;  
public const int WM\_CAP\_SET\_CALLBACK\_FRAME = WM\_USER + 5;  
// public const int WM\_CAP\_SET\_PREVIEW = WM\_USER + 50;  
// public const int WM\_CAP\_SET\_PREVIEWRATE = WM\_USER + 52;  
// public const int WM\_CAP\_SET\_VIDEOFORMAT = WM\_USER + 45;  
// public const int WM\_CAP\_START = WM\_USER;  
public const int WM\_CAP\_SAVEDIB = WM\_CAP\_START + 25;

public const string avicap32 = "avicap32.dll";  
public const int WM\_USER = 1024;  
/// &lt;summary&gt;  
///WM\_CAP\_START=WM\_USER=1024  
/// &lt;/summary&gt;  
public const int WM\_CAP\_START = WM\_USER;

// start of unicode messages  
/// &lt;summary&gt;  
/// 开始 WM\_USER + 100=1124  
/// &lt;/summary&gt;  
public const int WM\_CAP\_UNICODE\_START = WM\_USER + 100; //开始 1124  
/// &lt;summary&gt;  
/// /获得 CAPSTR EAMPTR  
/// WM\_CAP\_START + 1=1025  
/// &lt;/summary&gt;  
public const int WM\_CAP\_GET\_CAPSTREAMPTR = (WM\_CAP\_START + 1); //获得 CAPSTR EAMPTR  
/// &lt;summary&gt;  
/// 设置收回错误 WM\_CAP\_START + 2=1026  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_CALLBACK\_ERROR = (WM\_CAP\_START + 2); //设置收回错误  
/// &lt;summary&gt;  
/// 设置收回状态 WM\_CAP\_START + 3=1027  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_CALLBACK\_STATUS = (WM\_CAP\_START + 3); //设置收回状态  
/// &lt;summary&gt;  
/// 设置收回出产 WM\_CAP\_START + 4=1028  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_CALLBACK\_YIELD = (WM\_CAP\_START + 4); //设置收回出产  
/// &lt;summary&gt;  
/// 设置收回结构 WM\_CAP\_START + 5=1029  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_CALLBACK\_FRame = (WM\_CAP\_START + 5); //设置收回结构  
/// &lt;summary&gt;  
/// 设置收回视频流 WM\_CAP\_START + 6=1030  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_CALLBACK\_VIDEOSTREAM = (WM\_CAP\_START + 6); //设置收回视频流  
/// &lt;summary&gt;  
/// 设置收回视频波流 WM\_CAP\_START +7=1031  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_CALLBACK\_WAVESTREAM = (WM\_CAP\_START + 7); //设置收回视频波流  
/// &lt;summary&gt;  
/// 获得使用者数据 WM\_CAP\_START + 8=1032  
/// &lt;/summary&gt;  
public const int WM\_CAP\_GET\_USER\_DATA = (WM\_CAP\_START + 8); //获得使用者数据  
/// &lt;summary&gt;  
/// 设置使用者数据 WM\_CAP\_START + 9=1033  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_USER\_DATA = (WM\_CAP\_START + 9); //设置使用者数据  
/// &lt;summary&gt;  
/// 驱动程序连接 WM\_CAP\_START + 10=1034  
/// &lt;/summary&gt;  
public const int WM\_CAP\_DRIVER\_CONNECT = (WM\_CAP\_START + 10); //驱动程序连接  
/// &lt;summary&gt;  
/// 断开启动程序连接 WM\_CAP\_START + 11=1035  
/// &lt;/summary&gt;  
public const int WM\_CAP\_DRIVER\_DISCONNECT = (WM\_CAP\_START + 11); //断开启动程序连接  
/// &lt;summary&gt;  
/// 获得驱动程序名字 WM\_CAP\_START + 12=1036  
/// &lt;/summary&gt;  
public const int WM\_CAP\_DRIVER\_GET\_NAME = (WM\_CAP\_START + 12); //获得驱动程序名字  
/// &lt;summary&gt;  
/// 获得驱动程序版本 WM\_CAP\_START + 13=1037  
/// &lt;/summary&gt;  
public const int WM\_CAP\_DRIVER\_GET\_VERSION = (WM\_CAP\_START + 13); //获得驱动程序版本  
/// &lt;summary&gt;  
/// 获得驱动程序帽子 WM\_CAP\_START + 14=1038  
/// &lt;/summary&gt;  
public const int WM\_CAP\_DRIVER\_GET\_CAPS = (WM\_CAP\_START + 14); //获得驱动程序帽子  
/// &lt;summary&gt;  
/// 设置捕获文件 WM\_CAP\_START + 20=1044  
/// &lt;/summary&gt;  
public const int WM\_CAP\_FILE\_SET\_CAPTURE\_FILE = (WM\_CAP\_START + 20); //设置捕获文件  
/// &lt;summary&gt;  
/// 获得捕获文件 WM\_CAP\_START + 21=1045  
/// &lt;/summary&gt;  
public const int WM\_CAP\_FILE\_GET\_CAPTURE\_FILE = (WM\_CAP\_START + 21); //获得捕获文件  
/// &lt;summary&gt;  
/// 另存文件为 WM\_CAP\_START + 23=1047  
/// &lt;/summary&gt;  
public const int WM\_CAP\_FILE\_SAVEAS = (WM\_CAP\_START + 23); //另存文件为  
/// &lt;summary&gt;  
/// 保存文件 WM\_CAP\_START + 25=1049  
/// &lt;/summary&gt;  
public const int WM\_CAP\_FILE\_SAVEDIB = (WM\_CAP\_START + 25); //保存文件

// out of order to save on ifdefs  
/// &lt;summary&gt;  
/// 分派文件 WM\_CAP\_START + 22=1044  
/// &lt;/summary&gt;  
public const int WM\_CAP\_FILE\_ALLOCATE = (WM\_CAP\_START + 22); //分派文件  
/// &lt;summary&gt;  
/// 设置开始文件 WM\_CAP\_START + 24=1046  
/// &lt;/summary&gt;  
public const int WM\_CAP\_FILE\_SET\_INFOCHUNK = (WM\_CAP\_START + 24); //设置开始文件  
/// &lt;summary&gt;  
/// 编辑复制 WM\_CAP\_START + 30=1054  
/// &lt;/summary&gt;  
public const int WM\_CAP\_EDIT\_COPY = (WM\_CAP\_START + 30); //编辑复制  
/// &lt;summary&gt;  
/// 设置音频格式 WM\_CAP\_START + 35=1059  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_AUDIOFORMAT = (WM\_CAP\_START + 35); //设置音频格式  
/// &lt;summary&gt;  
/// 捕获音频格式 WM\_CAP\_START + 36=1060  
/// &lt;/summary&gt;  
public const int WM\_CAP\_GET\_AUDIOFORMAT = (WM\_CAP\_START + 36); //捕获音频格式  
/// &lt;summary&gt;  
/// 打开视频格式设置对话框 WM\_CAP\_START + 41=1065  
/// &lt;/summary&gt;  
public const int WM\_CAP\_DLG\_VIDEOFORMAT = (WM\_CAP\_START + 41); //1065 打开视频格式设置对话框  
/// &lt;summary&gt;  
/// 打开属性设置对话框，设置对比度、亮度等 WM\_CAP\_START + 42=1066  
/// &lt;/summary&gt;  
public const int WM\_CAP\_DLG\_VIDEOSOURCE = (WM\_CAP\_START + 42); //1066 打开属性设置对话框，设置对比度、亮度等。  
/// &lt;summary&gt;  
/// 打开视频显示 WM\_CAP\_START + 43=1067  
/// &lt;/summary&gt;  
public const int WM\_CAP\_DLG\_VIDEODISPLAY = (WM\_CAP\_START + 43); //1067 打开视频显示  
/// &lt;summary&gt;  
/// 获得视频格式 WM\_CAP\_START + 44=1068  
/// &lt;/summary&gt;  
public const int WM\_CAP\_GET\_VIDEOFORMAT = (WM\_CAP\_START + 44); //1068 获得视频格式  
/// &lt;summary&gt;  
/// 设置视频格式 WM\_CAP\_START + 45=1069  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_VIDEOFORMAT = (WM\_CAP\_START + 45); //1069 设置视频格式  
/// &lt;summary&gt;  
/// 打开压缩设置对话框 WM\_CAP\_START + 46=1070  
/// &lt;/summary&gt;  
public const int WM\_CAP\_DLG\_VIDEOCOMPRESSION = (WM\_CAP\_START + 46); //1070 打开压缩设置对话框  
/// &lt;summary&gt;  
/// 设置预览 WM\_CAP\_START + 50=1074  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_PREVIEW = (WM\_CAP\_START + 50); //设置预览  
/// &lt;summary&gt;  
/// 设置覆盖 WM\_CAP\_START + 51=1075  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_OVERLAY = (WM\_CAP\_START + 51); //设置覆盖  
/// &lt;summary&gt;  
/// 设置预览比例 WM\_CAP\_START + 52=1076  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_PREVIEWRATE = (WM\_CAP\_START + 52); //设置预览比例  
/// &lt;summary&gt;  
/// 设置刻度 WM\_CAP\_START + 53=1077  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_SCALE = (WM\_CAP\_START + 53); //设置刻度  
/// &lt;summary&gt;  
/// 获得状态 WM\_CAP\_START + 54=1078  
/// &lt;/summary&gt;  
public const int WM\_CAP\_GET\_STATUS = (WM\_CAP\_START + 54); //获得状态  
/// &lt;summary&gt;  
/// 设置卷 WM\_CAP\_START + 55=1079  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_SCROLL = (WM\_CAP\_START + 55); //设置卷  
/// &lt;summary&gt;  
/// 逮捕结构 WM\_CAP\_START + 60=1084  
/// &lt;/summary&gt;  
public const int WM\_CAP\_GRAB\_FRame = (WM\_CAP\_START + 60); //逮捕结构  
/// &lt;summary&gt;  
/// 停止逮捕结构 WM\_CAP\_START + 61=1085  
/// &lt;/summary&gt;  
public const int WM\_CAP\_GRAB\_FRame\_NOSTOP = (WM\_CAP\_START + 61); //停止逮捕结构  
/// &lt;summary&gt;  
/// 次序 WM\_CAP\_START + 62=1086  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SEQUENCE = (WM\_CAP\_START + 62); //次序  
/// &lt;summary&gt;  
/// 没有文件 WM\_CAP\_START + 63=1087  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SEQUENCE\_NOFILE = (WM\_CAP\_START + 63); //没有文件  
/// &lt;summary&gt;  
/// 设置安装次序 WM\_CAP\_START + 64=1088  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_SEQUENCE\_SETUP = (WM\_CAP\_START + 64); //设置安装次序  
/// &lt;summary&gt;  
/// 获得安装次序 WM\_CAP\_START + 65=1089  
/// &lt;/summary&gt;  
public const int WM\_CAP\_GET\_SEQUENCE\_SETUP = (WM\_CAP\_START + 65); //获得安装次序  
/// &lt;summary&gt;  
/// 设置媒体控制接口 WM\_CAP\_START + 66=1090  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_MCI\_DEVICE = (WM\_CAP\_START + 66); //设置媒体控制接口  
/// &lt;summary&gt;  
/// 获得媒体控制接口 WM\_CAP\_START + 67=1091  
/// &lt;/summary&gt;  
public const int WM\_CAP\_GET\_MCI\_DEVICE = (WM\_CAP\_START + 67); //获得媒体控制接口  
/// &lt;summary&gt;  
/// 停止 WM\_CAP\_START + 68=1092  
/// &lt;/summary&gt;  
public const int WM\_CAP\_STOP = (WM\_CAP\_START + 68); //停止  
/// &lt;summary&gt;  
/// 异常中断 WM\_CAP\_START + 69=1093  
/// &lt;/summary&gt;  
public const int WM\_CAP\_ABORT = (WM\_CAP\_START + 69); //异常中断  
/// &lt;summary&gt;  
/// 打开单一的结构 WM\_CAP\_START + 68=1094  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SINGLE\_FRame\_OPEN = (WM\_CAP\_START + 70); //打开单一的结构  
/// &lt;summary&gt;  
/// 关闭单一的结构 WM\_CAP\_START + 71=1095  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SINGLE\_FRame\_CLOSE = (WM\_CAP\_START + 71); //关闭单一的结构  
/// &lt;summary&gt;  
/// 单一的结构 WM\_CAP\_START + 72=1096  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SINGLE\_FRame = (WM\_CAP\_START + 72); //单一的结构  
/// &lt;summary&gt;  
/// 打开视频 WM\_CAP\_START + 80=1104  
/// &lt;/summary&gt;  
public const int WM\_CAP\_PAL\_OPEN = (WM\_CAP\_START + 80); //打开视频  
/// &lt;summary&gt;  
/// 保存视频 WM\_CAP\_START + 81=1105  
/// &lt;/summary&gt;  
public const int WM\_CAP\_PAL\_SAVE = (WM\_CAP\_START + 81); //保存视频  
/// &lt;summary&gt;  
/// 粘贴视频 WM\_CAP\_START + 82=1106  
/// &lt;/summary&gt;  
public const int WM\_CAP\_PAL\_PASTE = (WM\_CAP\_START + 82); //粘贴视频  
/// &lt;summary&gt;  
/// 自动创造 WM\_CAP\_START + 83=1107  
/// &lt;/summary&gt;  
public const int WM\_CAP\_PAL\_AUTOCREATE = (WM\_CAP\_START + 83); //自动创造  
/// &lt;summary&gt;  
/// 手动创造 WM\_CAP\_START + 84=1108  
/// &lt;/summary&gt;  
public const int WM\_CAP\_PAL\_MANUALCREATE = (WM\_CAP\_START + 84); //手动创造

// Following added post VFW 1.1  
/// &lt;summary&gt;  
/// 设置收回的错误 WM\_CAP\_START + 85=1109  
/// &lt;/summary&gt;  
public const int WM\_CAP\_SET\_CALLBACK\_CAPCONTROL = (WM\_CAP\_START + 85); // 设置收回的错误

public const int WM\_CAP\_END = WM\_CAP\_SET\_CALLBACK\_CAPCONTROL;

public delegate void FrameEventHandler(IntPtr lwnd, IntPtr lpVHdr);

#region 公共函数  
//公共函数  
public static object GetStructure(IntPtr ptr, ValueType structure)  
{  
return Marshal.PtrToStructure(ptr, structure.GetType());  
}

public static object GetStructure(int ptr, ValueType structure)  
{  
return GetStructure(new IntPtr(ptr), structure);  
}

public static void Copy(IntPtr ptr, byte\[] data)  
{  
Marshal.Copy(ptr, data, 0, data.Length);  
}

public static void Copy(int ptr, byte\[] data)  
{  
Copy(new IntPtr(ptr), data);  
}

public static int SizeOf(object structure)  
{  
return Marshal.SizeOf(structure);  
}  
#endregion 公共函数  
#region 结构 VIDEOHDR|BITMAPINFOHEADER|BITMAPINFO|CAPTUREPARMS|CAPDRIVERCAPS|CAPSTATUS  
//========================================================VideoHdr 结构=====================================================================  
//VideoHdr 结构 定义了视频数据块的头信息，在编写回调函数时常用到其数据成员lpData（指向数据缓存的指针）和dwBufferLength（数据缓存的大小）。  
//视频帧到缓存的捕获则需要应用回调函数和相应的数据块结构 VIDEOHDR  
\[StructLayout(LayoutKind.Sequential)]  
public struct VIDEOHDR  
{  
public IntPtr lpData; /* 指向数据缓存的指针 \*/  
public int dwBufferLength; /* 数据缓存的大小 \*/  
public int dwBytesUsed; /* Bytes actually used \*/  
public int dwTimeCaptured; /* Milliseconds from start of stream \*/  
public int dwUser; /* for client's use \*/  
public int dwFlags; /* assorted flags (see defines) \*/  
public int dwReserved; /* reserved for driver \*/  
}  
//=======================================================BitmapInfoHeader结构===================================================================  
//BitmapInfoHeader定义了位图的头部信息  
\[StructLayout(LayoutKind.Sequential)]  
public struct BITMAPINFOHEADER  
{  
public int biSize;  
public int biWidth;  
public int biHeight;  
public short biPlanes;  
public short biBitCount;  
public int biCompression;  
public int biSizeImage;  
public int biXPelsPerMeter;  
public int biYPelsPerMeter;  
public int biClrUsed;  
public int biClrImportant;  
}  
//=========================================================================================================================================

//======================================================BitmapInfo结构=====================================================================  
//BitmapInfo 位图信息  
\[StructLayout(LayoutKind.Sequential)]  
public struct BITMAPINFO  
{  
public BITMAPINFOHEADER bmiHeader;  
public int bmiColors;  
}

//=====================================================CAPTUREPARMS结构======================================================================  
//CAPTUREPARMS 包含控制视频流捕获过程的参数，如捕获帧频、指定键盘或鼠标键以终止捕获、捕获时间限制等；  
\[StructLayout(LayoutKind.Sequential)]  
public struct CAPTUREPARMS  
{  
public int dwRequestMicroSecPerFrame; // 期望的桢播放率，以毫秒为单位，默认为66667，相当于15桢每秒。  
public bool fMakeUserHitOKToCapture; // Show "Hit OK to cap" dlg?开始捕获标志位，如果值为真，则在开始捕获前要产生一个询问对话框，默认为假。  
public uint wPercentDropForError; //所允许的最大丢桢百分比，可以从0变化到100，默认值为10。  
public bool fYield; /\*另起线程标志位，如果为真，则程序重新启动一个线程用于视频流的捕获，默认值是假。  
但是如果你是为了真，你必须要在程序中处理一些潜在的操作，因为当视频捕获时，其他操作并没有被屏蔽。\*/  
public int dwIndexSize; // 在AVI文件中所允许的最大数目的索引项(32K)  
public uint wChunkGranularity; // AVI文件的逻辑尺寸，以字节为单位。如果值是0，则说明该尺寸渐增 在 Win32程序中无用。(2K)  
public bool fUsingDOSMemory; // Use DOS buffers?  
public uint wNumVideoRequested; // 所被允许分配的最大视频缓存  
public bool fCaptureAudio; // 音频标志位，如果音频流正在捕获，则该值为真。  
public uint wNumAudioRequested; // 最大数量的音频缓存，默认值为10。  
public uint vKeyAbort; // 终止流捕获的虚拟键盘码，默认值为VK\_ESCAPE  
\[MarshalAs(UnmanagedType.Bool)]  
public bool fAbortLeftMouse; // 终止鼠标左键标志位，如果该值为真，则在流捕获过程中如果点击鼠标左键则该捕获终止，默认值为真。  
public bool fAbortRightMouse; // Abort on right mouse?  
public bool fLimitEnabled; // 捕获操作时间限制，如果为真，则时间到了以后捕获操作终止，默认为假  
public uint wTimeLimit; // 具体终止时间，只有 fLimitEnabled是真时.该位才有效  
public bool fMCIControl; // Use MCI video source?  
public bool fStepMCIDevice; // Step MCI device?MCI 设备标志。  
public int dwMCIStartTime; // Time to start in MS  
public int dwMCIStopTime; // Time to stop in MS  
public bool fStepCaptureAt2x; // Perform spatial averaging 2x  
public int wStepCaptureAverageFrames; // 当基于平均采样来创建桢时，桢的采样时间，典型值是5  
public int dwAudioBufferSize; // 音频缓存的尺寸，如果用默认值0，缓存尺寸是最大0.5秒，或10k字节。  
public int fDisableWriteCache; // Attempt to disable write cache  
public int AVStreamMaster; //音视频同步标志。  
}  
//=========================================================================================================================================

//=================================================CAPDRIVERCAPS结构=======================================================================  
//CAPDRIVERCAPS定义了捕获驱动器的能力，如有无视频叠加能力、有无控制视频源、视频格式的对话框等；  
\[StructLayout(LayoutKind.Sequential)]  
public struct CAPDRIVERCAPS  
{  
\[MarshalAs(UnmanagedType.U2)]  
public UInt16 wDeviceIndex; //捕获驱动器的索引值，该值可以由0到9变化。  
\[MarshalAs(UnmanagedType.Bool)]  
public bool fHasOverlay; // 视频叠加标志，如果设备支持视频叠加这该位是真。  
\[MarshalAs(UnmanagedType.Bool)]  
public bool fHasDlgVideoSource; //视频资源对话框标志位，如果设备支持视频选择、控制对话框，该值为真。  
\[MarshalAs(UnmanagedType.Bool)]  
public bool fHasDlgVideoFormat; //视频格式对话框标志位，如果设备支持对视频格式对话框的选择，该位真。  
\[MarshalAs(UnmanagedType.Bool)]  
public bool fHasDlgVideoDisplay; //视频展示对话框标志位，如果设备支持对视频捕获缓存区的重新播放，该位是真。  
\[MarshalAs(UnmanagedType.Bool)]  
public bool fCaptureInitialized; //捕获安装标志位，如果捕获驱动器已经成功连接，该值为真。  
//\[MarshalAs(UnmanagedType.Bool)]  
public bool fDriverSuppliesPalettes; //驱动器调色板标志位，如果驱动器能创建调色板，则该位是真。  
\[MarshalAs(UnmanagedType.I4)]  
public int hVideoIn;  
\[MarshalAs(UnmanagedType.I4)]  
public int hVideoOut;  
\[MarshalAs(UnmanagedType.I4)]  
public int hVideoExtIn;  
\[MarshalAs(UnmanagedType.I4)]  
public int hVideoExtOut;  
}  
//=========================================================================================================================================

//=====================================================CAPSTATUS结构========================================================================  
//CAPSTATUS定义了捕获窗口的当前状态，如图像的宽、高等；  
\[StructLayout(LayoutKind.Sequential)]  
public struct CAPSTATUS  
{  
public int uiImageWidth; //图像宽度  
public int uiImageHeight; //图像高度  
public bool fLiveWindow; //活动窗口标记，如果窗口正以预览的方式展示图像，那么该值为真  
public bool fOverlayWindow; //叠加窗口标志位，如果正在使用硬件叠加，则该位是真。  
public bool fScale; //输入所放标志位，如果窗口是正在缩放视频到客户区，那么该位是真。当使用硬件叠加时，改位无效。  
public Point ptScroll; //被展示在窗口客户区左上角的那个象素的x、y坐标偏移量。  
public bool fUsingDefaultPalette; //默认调色板标志位，如果捕获窗口正在使用当前默认调色板，该值为真  
public bool fAudioHardware; // 音频硬件标志位，如果系统已经安装了音频硬件，该值为真。  
public bool fCapFileExists; //捕获文件标志位，如果一个捕获文件已经被创建，该值为真  
public int dwCurrentVideoFrame; // 当前或最近流捕获过程中，所处理的桢的数目。包括丢弃的桢。  
public int dwCurrentVideoFramesDropped;//当前流捕获过程中丢弃的桢的数目。  
public int dwCurrentWaveSamples; // # of wave samples cap'td  
public int dwCurrentTimeElapsedMS; // 从当前流捕获开始计算，程序所用的时间，以毫秒为单位。  
public IntPtr hPalCurrent; // 当前剪切板的句柄。  
public bool fCapturingNow; // 捕获标志位，当捕获是正在进行时，改位是真  
public int dwReturn; // 错误返回值，当你的应用程序不支持错误回调函数时可以应用改位  
public int wNumVideoAllocated; // 被分配的视频缓存的数目。  
public int wNumAudioAllocated; // 被分配的音频缓存的数目。  
}  
//=========================================================================================================================================

#endregion 结构 VIDEOHDR|BITMAPINFOHEADER|BITMAPINFO|CAPTUREPARMS|CAPDRIVERCAPS|CAPSTATUS

}  
public class cVideo //视频类  
{  
public bool flag = true;  
private IntPtr lwndC; //保存无符号句柄  
private IntPtr mControlPtr; //保存管理指示器  
private int mWidth;  
private int mHeight;  
public delegate void RecievedFrameEventHandler(byte\[] data);  
public event RecievedFrameEventHandler RecievedFrame;

public VideoAPI.CAPTUREPARMS Capparms;  
private VideoAPI.FrameEventHandler mFrameEventHandler;  
public VideoAPI.CAPDRIVERCAPS CapDriverCAPS;//捕获驱动器的能力，如有无视频叠加能力、有无控制视频源、视频格式的对话框等；  
public VideoAPI.CAPSTATUS CapStatus;//该结构用于保存视频设备捕获窗口的当前状态，如图像的宽、高等  
string strFileName;  
public cVideo(IntPtr handle, int width, int height)  
{  
CapDriverCAPS = new VideoAPI.CAPDRIVERCAPS();//捕获驱动器的能力，如有无视频叠加能力、有无控制视频源、视频格式的对话框等；  
CapStatus = new VideoAPI.CAPSTATUS();//该结构用于保存视频设备捕获窗口的当前状态，如图像的宽、高等  
mControlPtr = handle; //显示视频控件的句柄  
mWidth = width; //视频宽度  
mHeight = height; //视频高度  
}  
/// &lt;summary&gt;  
/// 打开视频设备  
/// &lt;/summary&gt;  
public bool StartWebCam()  
{  
//byte\[] lpszName = new byte\[100];  
//byte\[] lpszVer = new byte\[100];  
//VideoAPI.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100);  
//this.lwndC = VideoAPI.capCreateCaptureWindowA(lpszName, VideoAPI.WS\_CHILD | VideoAPI.WS\_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);  
//if (VideoAPI.SendMessage(lwndC, VideoAPI.WM\_CAP\_DRIVER\_CONNECT, 0, 0))  
//{  
// VideoAPI.SendMessage(lwndC, VideoAPI.WM\_CAP\_SET\_PREVIEWRATE, 100, 0);  
// VideoAPI.SendMessage(lwndC, VideoAPI.WM\_CAP\_SET\_PREVIEW, true, 0);  
// return true;  
//}  
//else  
//{  
// return false;  
//}  
this.lwndC = VideoAPI.capCreateCaptureWindow("", VideoAPI.WS\_CHILD | VideoAPI.WS\_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);//AVICap类的捕捉窗口  
VideoAPI.FrameEventHandler FrameEventHandler = new VideoAPI.FrameEventHandler(FrameCallback);  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SET\_CALLBACK\_ERROR, 0, 0);//注册错误回调函数  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SET\_CALLBACK\_STATUS, 0, 0);//注册状态回调函数  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SET\_CALLBACK\_VIDEOSTREAM, 0, 0);//注册视频流回调函数  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SET\_CALLBACK\_FRAME, 0, FrameEventHandler);//注册帧回调函数

//if (!CapDriverCAPS.fCaptureInitialized)//判断当前设备是否被其他设备连接已经连接  
//{

if (VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_DRIVER\_CONNECT, 0, 0))  
{  
//-----------------------------------------------------------------------  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_DRIVER\_GET\_CAPS, VideoAPI.SizeOf(CapDriverCAPS), ref CapDriverCAPS);//获得当前视频 CAPDRIVERCAPS定义了捕获驱动器的能力，如有无视频叠加能力、有无控制视频源、视频格式的对话框等；  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_GET\_STATUS, VideoAPI.SizeOf(CapStatus), ref CapStatus);//获得当前视频流的尺寸 存入CapStatus结构

VideoAPI.BITMAPINFO bitmapInfo = new VideoAPI.BITMAPINFO();//设置视频格式 (height and width in pixels, bits per frame).  
bitmapInfo.bmiHeader = new VideoAPI.BITMAPINFOHEADER();  
bitmapInfo.bmiHeader.biSize = VideoAPI.SizeOf(bitmapInfo.bmiHeader);  
bitmapInfo.bmiHeader.biWidth = mWidth;  
bitmapInfo.bmiHeader.biHeight = mHeight;  
bitmapInfo.bmiHeader.biPlanes = 1;  
bitmapInfo.bmiHeader.biBitCount = 24;  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SET\_PREVIEWRATE, 40, 0);//设置在PREVIEW模式下设定视频窗口的刷新率 设置每40毫秒显示一帧，即显示帧速为每秒25帧  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SET\_SCALE, 1, 0);//打开预览视频的缩放比例  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SET\_VIDEOFORMAT, VideoAPI.SizeOf(bitmapInfo), ref bitmapInfo);

this.mFrameEventHandler = new VideoAPI.FrameEventHandler(FrameCallback);  
this.capSetCallbackOnFrame(this.lwndC, this.mFrameEventHandler);

VideoAPI.CAPTUREPARMS captureparms = new VideoAPI.CAPTUREPARMS();  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_GET\_SEQUENCE\_SETUP, VideoAPI.SizeOf(captureparms), ref captureparms);  
if (CapDriverCAPS.fHasOverlay)  
{  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SET\_OVERLAY, 1, 0);//启用叠加 注：据说启用此项可以加快渲染速度  
}  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SET\_PREVIEW, 1, 0);//设置显示图像启动预览模式 PREVIEW  
VideoAPI.SetWindowPos(this.lwndC, 0, 0, 0, mWidth, mHeight, VideoAPI.SWP\_NOZORDER | VideoAPI.SWP\_NOMOVE);//使捕获窗口与进来的视频流尺寸保持一致  
return true;  
}  
else  
{

flag = false;  
return false;  
}  
}  
public void get()  
{  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_GET\_SEQUENCE\_SETUP, VideoAPI.SizeOf(Capparms), ref Capparms);  
}  
public void set()  
{  
VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SET\_SEQUENCE\_SETUP, VideoAPI.SizeOf(Capparms), ref Capparms);  
}  
private bool capSetCallbackOnFrame(IntPtr lwnd, VideoAPI.FrameEventHandler lpProc)  
{  
return VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SET\_CALLBACK\_FRAME, 0, lpProc);  
}  
/// &lt;summary&gt;  
/// 关闭视频设备  
/// &lt;/summary&gt;  
public void CloseWebcam()  
{  
VideoAPI.SendMessage(lwndC, VideoAPI.WM\_CAP\_DRIVER\_DISCONNECT, 0, 0);  
}  
/// &lt;summary&gt;  
/// 拍照  
/// &lt;/summary&gt;  
/// &lt;param&gt;要保存bmp文件的路径&lt;/param&gt;  
public void GrabImage(IntPtr hWndC, string path)  
{  
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);  
VideoAPI.SendMessage(lwndC, VideoAPI.WM\_CAP\_SAVEDIB, 0, hBmp.ToInt32());  
}  
public void StarKinescope(string path)  
{  
strFileName = path;  
string dir = path.Remove(path.LastIndexOf("//"));  
if (!File.Exists(dir))  
{  
Directory.CreateDirectory(dir);  
}  
int hBmp = Marshal.StringToHGlobalAnsi(path).ToInt32();  
bool b = VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_FILE\_SET\_CAPTURE\_FILE, 0, hBmp);  
b = VideoAPI.SendMessage(this.lwndC, VideoAPI.WM\_CAP\_SEQUENCE, 0, 0);  
}  
/// &lt;summary&gt;  
/// 停止录像  
/// &lt;/summary&gt;  
public void StopKinescope()  
{  
VideoAPI.SendMessage(lwndC, VideoAPI.WM\_CAP\_STOP, 0, 0);  
}  
private void FrameCallback(IntPtr lwnd, IntPtr lpvhdr)  
{  
VideoAPI.VIDEOHDR videoHeader = new VideoAPI.VIDEOHDR();  
byte\[] VideoData;  
videoHeader = (VideoAPI.VIDEOHDR)VideoAPI.GetStructure(lpvhdr, videoHeader);  
VideoData = new byte\[videoHeader.dwBytesUsed];  
VideoAPI.Copy(videoHeader.lpData, VideoData);  
if (this.RecievedFrame != null)  
this.RecievedFrame(VideoData);  
}  
}

}