摄像头程序
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Pick
{
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;
public static int WM_CAP_DLG_VIDEOSOURCE = (WM_CAP_START + 42); //选择
摄像头
private IntPtr hWndC;
private bool bStat = false;
private IntPtr mControlPtr;
private int mWidth;
private int mHeight;
private int mLeft;
private int mTop;
///
/// 初始化摄像头
///
///
控件的句柄
///
开始显示的左边距
///
开始显示的上边距
///
要显示的宽度
///
要显示的长度
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,
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);
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);
}
return;
}
///
/// 停止显示
///
public void Stop()
{
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
bStat = false;
}
///
/// 抓图
///
///
要保存bmp文件的路径
public void GrabImage(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC, WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
}
///
/// 录像
///
///
要保存avi文件的路径
public void Kinescope(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt32());
SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
}
///
/// 停止录像
///
public void StopKinescope()
{
SendMessage(hWndC, WM_CAP_STOP, 0, 0);
}
///
///
///
public void SetShow()
{
SendMessage(hWndC, WM_CAP_DLG_VIDEOSOURCE, 0, 0);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
SaveFileDialog1.Filter = "bmp files (*.bmp)|*.bmp|jpg files (*
.jpg)|*.jpg|All files (*.*)|*.* ";
SaveFileDialog1.FilterIndex = 2; //默认图片保存格
式 JPG
SaveFileDialog1.RestoreDirectory = true;
SaveFileDialog1.FileName = "Pic01 "; //默认图片保存名称 Pic01
if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
{
Form1.Pick pk = new Pick(Handle, panel1.Left, panel1.T
op, panel1.Width, panel1.Height);
pk.GrabImage(SaveFileDialog1.FileName);
}
}
catch
{
}
}
private void button2_Click(object sender, EventArgs e)
{
Form1.Pick pk = new Pick(Handle, panel1.Left, panel1.Top, panel1.Wid
th, panel1.Height);
pk.Start();
}
private void button3_Click(object sender, EventArgs e)
{
Form1.Pick pk = new Pick(Handle, panel1.Left, panel1.Top, panel1.Wid
th, panel1.Height);
pk.Stop();
}
private void button4_Click(object sender, EventArgs e)
{
Form1.Pick pk = new Pick(Handle, panel1.Left, panel1.Top, panel1.Wid
th, panel1.Height);
pk.GrabImage( "c:\001.bmp ");
}
private void button5_Click(object sender, EventArgs e)
{
Form1.Pick pk = new Pick(Handle, panel1.Left, panel1.Top, panel1.Width, pa
nel1.Height);
pk.Kinescope( "c:\\001.avi ");
}
private void button6_Click(object sender, EventArgs e)
{
Form1.Pick pk = new Pick(Handle, panel1.Left, panel1.Top, panel1.Wid
th, panel1.Height);
pk.StopKinescope();
}
获取摄像头的代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
namespace CapTureMovie
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button BtnCapTure;
private System.Windows.Forms.Panel panel_Vedio;
private int hHwnd;
private System.Windows.Forms.Button BtnStop;
private System.Windows.Forms.Label LbSysMsg;
private System.Windows.Forms.Button button1;
public struct videohdr_tag
{
public byte[] lpData;
public int dwBufferLength;
public int dwBytesUsed;
public int dwTimeCaptured;
public int dwUser;
public int dwFlags;
public int[] dwReserved;
}
public delegate bool CallBack(int hwnd, int lParam);
///
/// 必需的器变量。
///
private System.ComponentModel.Container components = null;
[DllImport( "avicap32.dll ", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] public static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
[DllImport( "avicap32.dll ", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] public static extern bool capGetDriverDescriptionA(short wDriver, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszName, int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszVer, int cbVer);
[DllImport( "user32 ", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] public static extern bool DestroyWindow(int hndw);
[DllImport( "user32 ", EntryPoint= "SendMessageA
", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
[DllImport( "user32 ", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport( "vfw32.dll ")]
public static extern string capVideoStreamCallback(int hwnd,videohdr_tag videohdr_tag);
[DllImport( "vicap32.dll ", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern bool capSetCallbackOnFrame(int hwnd,string s);
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing ) {
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.panel_Vedio = new System.Windows.Forms.Panel(); this.BtnCapTure = new System.Windows.Forms.Button(); this.BtnStop = new System.Windows.Forms.Button(); this.LbSysMsg = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout();
//
// panel_Vedio
//
this.panel_Vedio.BackgroundImage = ((System.Drawing.Image)(resources.GetObject( "panel_Vedio.BackgroundI
mage ")));
this.panel_Vedio.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel_Vedio.Location = new System.Drawing.Point(8, 16); this.panel_Vedio.Name = "panel_Vedio ";
this.panel_Vedio.Size = new System.Drawing.Size(288, 224); this.panel_Vedio.TabIndex = 0;
//
// BtnCapTure
//
this.BtnCapTure.Location = new System.Drawing.Point(24, 256); this.BtnCapTure.Name = "BtnCapTure ";
this.BtnCapTure.TabIndex = 1;
this.BtnCapTure.Text = "图象采集 ";
this.BtnCapTure.Click += new System.EventHandler(this.BtnCapTure_Click);
//
// BtnStop
//
this.BtnStop.Enabled = false;
this.BtnStop.Location = new System.Drawing.Point(136, 256); this.BtnStop.Name = "BtnStop ";
this.BtnStop.TabIndex = 1;
this.BtnStop.Text = "停止采集 ";
this.BtnStop.Click += new System.EventHandler(this.BtnStop_Click); //
// LbSysMsg
//
this.LbSysMsg.Location = new System.Drawing.Point(16, 296); this.LbSysMsg.Name = "LbSysMsg ";
this.LbSysMsg.Size = new System.Drawing.Size(240, 23); this.LbSysMsg.TabIndex = 2;
//
// button1
//
this.button1.Location = new System.Drawing.Point(304, 48); this.button1.Name = "button1 ";
this.button1.TabIndex = 3;
this.button1.Text = "button1 ";
this.button1.Click += new System.EventHandler(this.button1_Click); //
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(400, 357); this.Controls.Add(this.button1);
this.Controls.Add(this.LbSysMsg);
this.Controls.Add(this.BtnCapTure);
this.Controls.Add(this.panel_Vedio);
this.Controls.Add(this.BtnStop);
this.Name = "Form1 ";
this.Text = "Form1 ";
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void BtnCapTure_Click(object sender, System.EventArgs e) {
this.OpenCapture();
}
private void OpenCapture()
{
int intWidth=this.panel_Vedio.Width;
int intHeight=this.panel_Vedio.Height;
int intDevice=0;
string refDevice=intDevice.ToString();
hHwnd=Form1.capCreateCaptureWindowA(ref refDevice,1342177280,0,0,640,480,this.panel_Vedio.Handle.ToInt32()
,0);
this.LbSysMsg.Text= " ";
this.LbSysMsg.Text+= "驱动: "+refDevice;
if(Form1.SendMessage(hHwnd,0x40a,intDevice,0)> 0) {
Form1.SendMessage(this.hHwnd,0x435,-1, 0);
Form1.SendMessage(this.hHwnd,0x434,0x42, 0);
Form1.SendMessage(this.hHwnd,0x432,-1, 0);
Form1.SetWindowPos(this.hHwnd,1,0,0,intWidth,intHeight,6); this.BtnCapTure.Enabled=false;
this.BtnStop.Enabled=true;
}
else
{
Form1.DestroyWindow(this.hHwnd);
this.BtnCapTure.Enabled=false;
this.BtnStop.Enabled=true;
}
}
private void BtnStop_Click(object sender, System.EventArgs e) {
Form1.SendMessage(this.hHwnd, 0x40b, 0, 0); Form1.DestroyWindow(this.hHwnd);
this.BtnCapTure.Enabled=true;
this.BtnStop.Enabled=false;
}
private void button1_Click(object sender, System.EventArgs e) {
try
{
Form1.SendMessage(this.hHwnd,0x41e,0,0);
IDataObject obj1 = Clipboard.GetDataObject(); if (obj1.GetDataPresent(typeof(Bitmap)))
{
Image image1 = (Image) obj1.GetData(typeof(Bitmap));
// this.panel_Vedio.Image = image1;
// this.ClosePreviewWindow();
SaveFileDialog SaveFileDialog1=new SaveFileDialog();
if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
{
image1.Save(SaveFileDialog1.FileName, ImageFormat.Bmp);
}
}
}
catch
{ }
} } }