文件:VedioCapture2.cs: using System; using System.Runtime.InteropServices; namespace Wuyin.ShoesManager { /// <summary> /// VedioCapture 的摘要说明。 /// </summary> public class VedioCapture { private int hCaptureM; private bool isUnLoad = false; public VedioCapture() { } [DllImport("avicap32.dll")] private static extern int capCreateCaptureWindow( string strWindowName, int dwStyle, int x, int y ,int width, int height , int hwdParent, int nID ); [DllImport("user32.dll")] private static extern int SendMessage( int hwnd , int wMsg, int wParam , int lParam ); [DllImport("user32.dll")] private static extern int SendMessage( int hwnd , int wMsg, int wParam , string lParam ); [DllImport("Kernel32.dll")] private static extern bool CloseHandle( int hObject ); public bool Initialize( System.Windows.Forms.Control aContainer , int intWidth, int intHeight ) { hCaptureM = capCreateCaptureWindow( "", 0x40000000 | 0x10000000, 0,0,intWidth,intHeight,aContainer.Handle.ToInt32() ,1 ); if( hCaptureM == 0 ) return false; int ret = SendMessage( hCaptureM , 1034, 0,0 ); if( ret == 0 ) { CloseHandle(hCaptureM); return false; } //WM_CAP_SET_PREVIEW ret = SendMessage( hCaptureM, 1074, 1, 0 ); if( ret == 0 ) { this.UnLoad(); return false; } //WM_CAP_SET_SCALE ret = SendMessage( hCaptureM, 1077, 1, 0 ); if( ret == 0 ) { this.UnLoad(); return false; } //WM_CAP_SET_PREVIEWRATE ret = SendMessage( hCaptureM, 1076, 66, 0 ); if( ret == 0 ) { this.UnLoad(); return false; } return true; } public void SingleFrameBegin() { // int ret = SendMessage( hCaptureM, 1094 , 0, 0 ); } public void SingleFrameEnd() { // int ret = SendMessage( hCaptureM, 1095 , 0, 0 ); } public void SingleFrameMode() { //WM_CAP_GRAB_FRAME int ret = SendMessage( hCaptureM, 1084 , 0, 0 ); //WM_CAP_SET_PREVIEW //int ret = SendMessage( hCaptureM, 1074 , 0, 0 ); //WM_CAP_SINGLE_FRAME //ret = SendMessage( hCaptureM, 1096 , 0, 0 ); } public void PreviewMode() { int ret = SendMessage( hCaptureM, 1074 , 1, 0 ); } public void UnLoad() { int ret = SendMessage( hCaptureM, 1035, 0, 0 ); CloseHandle( this.hCaptureM ); isUnLoad = true; } public void CopyToClipBorad() { int ret = SendMessage( hCaptureM, 1054, 0, 0 ); } public void ShowFormatDialog() { int ret = SendMessage( hCaptureM, 1065, 0, 0 ); } public void SaveToDIB( string fileName ) { int ret = SendMessage( hCaptureM, 1049, 0, fileName ); } public void ShowDisplayDialog() { int ret = SendMessage( hCaptureM, 1067, 0, 0 ); } public System.Drawing.Image getCaptureImage() { System.Windows.Forms.IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject(); System.Drawing.Image retImage = null; if( iData != null ) { if( iData.GetDataPresent( System.Windows.Forms.DataFormats.Bitmap ) ) { retImage = (System.Drawing.Image)iData.GetData( System.Windows.Forms.DataFormats.Bitmap ); } else if( iData.GetDataPresent( System.Windows.Forms.DataFormats.Dib ) ) { retImage = (System.Drawing.Image)iData.GetData( System.Windows.Forms.DataFormats.Dib ); } } return retImage; } ~VedioCapture() { if( !isUnLoad ) { this.UnLoad(); } } } } 窗体的cs: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; using webcam; using Wuyin.ShoesManager; using PickHead; namespace sxt { /// <summary> /// 摄像头采集保存照片程序。 /// VedioCapture.cs和VedioCapture2.cs是两种不同的摄像头采集代码。 /// 本程序用的是VedioCapture2.cs的方法,注释掉的部分是用VedioCapture.cs的方法 /// int64@163.com /// 2005年3月28日 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button b_play; private System.Windows.Forms.Panel panelPreview; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; private System.Windows.Forms.Button b_stop; private System.Windows.Forms.Button b_close; private System.Windows.Forms.Button b_getpic; private System.Windows.Forms.PictureBox pictureBox1; WebCamera wc; private System.Windows.Forms.SaveFileDialog saveFileDialog1; VedioCapture xxx=new VedioCapture(); private System.Windows.Forms.TextBox t_out; int savefiles; private System.Windows.Forms.Button b_save; private System.Windows.Forms.Button b_savepic; private System.Windows.Forms.Button button1; string filepath; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.b_play = new System.Windows.Forms.Button(); this.b_stop = new System.Windows.Forms.Button(); this.panelPreview = new System.Windows.Forms.Panel(); this.b_close = new System.Windows.Forms.Button(); this.b_getpic = new System.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.b_save = new System.Windows.Forms.Button(); this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); this.t_out = new System.Windows.Forms.TextBox(); this.b_savepic = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // b_play // this.b_play.Enabled = false; this.b_play.Location = new System.Drawing.Point(8, 312); this.b_play.Name = "b_play"; this.b_play.TabIndex = 0; this.b_play.Text = "&Play"; this.b_play.Click += new System.EventHandler(this.b_play_Click); // // b_stop // this.b_stop.Location = new System.Drawing.Point(96, 312); this.b_stop.Name = "b_stop"; this.b_stop.TabIndex = 1; this.b_stop.Text = "&Stop"; this.b_stop.Click += new System.EventHandler(this.b_stop_Click); // // panelPreview // this.panelPreview.Location = new System.Drawing.Point(8, 8); this.panelPreview.Name = "panelPreview"; this.panelPreview.Size = new System.Drawing.Size(352, 288); this.panelPreview.TabIndex = 2; // // b_close // this.b_close.Location = new System.Drawing.Point(648, 312); this.b_close.Name = "b_close"; this.b_close.TabIndex = 3; this.b_close.Text = "&Close"; this.b_close.Click += new System.EventHandler(this.b_close_Click); // // b_getpic // this.b_getpic.Location = new System.Drawing.Point(184, 312); this.b_getpic.Name = "b_getpic"; this.b_getpic.TabIndex = 5; this.b_getpic.Text = "&GetPic"; this.b_getpic.Click += new System.EventHandler(this.b_getpic_Click); // // pictureBox1 // this.pictureBox1.Location = new System.Drawing.Point(376, 8); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(352, 288); this.pictureBox1.TabIndex = 6; this.pictureBox1.TabStop = false; // // b_save // this.b_save.Location = new System.Drawing.Point(360, 312); this.b_save.Name = "b_save"; this.b_save.TabIndex = 7; this.b_save.Text = "S&ave"; this.b_save.Click += new System.EventHandler(this.b_save_Click); // // saveFileDialog1 // this.saveFileDialog1.FileName = "shipin.jpg"; this.saveFileDialog1.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*"; this.saveFileDialog1.RestoreDirectory = true; this.saveFileDialog1.Title = "保存文件"; // // t_out // this.t_out.BackColor = System.Drawing.SystemColors.InactiveBorder; this.t_out.ForeColor = System.Drawing.SystemColors.WindowText; this.t_out.Location = new System.Drawing.Point(8, 344); this.t_out.Name = "t_out"; this.t_out.Size = new System.Drawing.Size(712, 21); this.t_out.TabIndex = 8; this.t_out.Text = ""; // // b_savepic // this.b_savepic.Enabled = false; this.b_savepic.Location = new System.Drawing.Point(272, 312); this.b_savepic.Name = "b_savepic"; this.b_savepic.TabIndex = 9; this.b_savepic.Text = "Sa&vePic"; this.b_savepic.Click += new System.EventHandler(this.b_savepic_Click); // // button1 // this.button1.Location = new System.Drawing.Point(448, 312); this.button1.Name = "button1"; this.button1.TabIndex = 10; this.button1.Text = "button1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(744, 373); this.Controls.Add(this.button1); this.Controls.Add(this.b_savepic); this.Controls.Add(this.t_out); this.Controls.Add(this.b_save); this.Controls.Add(this.pictureBox1); this.Controls.Add(this.b_getpic); this.Controls.Add(this.b_close); this.Controls.Add(this.panelPreview); this.Controls.Add(this.b_stop); this.Controls.Add(this.b_play); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "我的摄像头"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion Top 回复人:int64(@163.com:所有的程序员都应该团结起来!) ( 五级(中级)) 信誉:100 2007-3-16 14:46:13 得分:0 ? /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { savefiles=1; filepath=""; //VedioCapture2.cs的方法 xxx.Initialize( panelPreview,panelPreview.Width,panelPreview.Height); //VedioCapture.cs的方法 /*wc = new WebCamera( panelPreview.Handle,panelPreview.Width,panelPreview.Height); wc.StartWebCam();*/ } private void b_play_Click(object sender, System.EventArgs e) { xxx.Initialize( panelPreview,panelPreview.Width,panelPreview.Height); panelPreview.Visible=true; b_play.Enabled = false; b_stop.Enabled = true; this.b_savepic.Enabled=false; this.b_save.Enabled=true; this.b_getpic.Enabled=true; this.t_out.Text="成功获取视频!"; /*wc = new WebCamera( panelPreview.Handle,panelPreview.Width,panelPreview.Height); wc.StartWebCam();*/ } private void b_stop_Click(object sender, System.EventArgs e) { xxx.UnLoad(); panelPreview.Visible=false; b_play.Enabled = true; b_stop.Enabled = false; this.b_save.Enabled=false; this.b_savepic.Enabled=false; this.b_getpic.Enabled=false; this.t_out.Text="停止获取视频!"; /* wc.CloseWebcam();*/ } private void b_close_Click(object sender, System.EventArgs e) { this.Close(); } private void b_getpic_Click(object sender, System.EventArgs e) { this.t_out.Text=""; xxx.CopyToClipBorad(); this.pictureBox1.Image = xxx.getCaptureImage(); this.b_savepic.Enabled=true; this.t_out.Text="获取图片成功!"; } private void b_savepic_Click(object sender, System.EventArgs e) { this.t_out.Text=""; string filename; if(filepath=="") { filename="图片"+System.DateTime.Now.Date.ToShortDateString(); this.saveFileDialog1.FileName=filename; if(saveFileDialog1.ShowDialog() == DialogResult.OK) filepath=this.saveFileDialog1.FileName; else return; } filename=""; if(savefiles<10) filename+="_00"+savefiles.ToString(); else if(savefiles<100) filename+="_0"+savefiles.ToString(); else filename+="_"+savefiles.ToString(); int leg=filepath.Length; filename=filepath.Substring(0,leg-4)+filename+filepath.Substring(leg-4,4); this.pictureBox1.Image.Save(filename); this.t_out.Text="保存图片“"+filename+"”成功!"; if(savefiles%2==0) this.t_out.ForeColor=Color.Red; else this.t_out.ForeColor=Color.Blue; savefiles++; } private void b_save_Click(object sender, System.EventArgs e) { this.t_out.Text=""; xxx.CopyToClipBorad(); this.pictureBox1.Image = xxx.getCaptureImage(); this.t_out.Text="获取图片成功!"; string filename; if(filepath=="") { filename="图片"+System.DateTime.Now.Date.ToShortDateString(); this.saveFileDialog1.FileName=filename; if(saveFileDialog1.ShowDialog() == DialogResult.OK) filepath=this.saveFileDialog1.FileName; else return; } filename=""; if(savefiles<10) filename+="_00"+savefiles.ToString(); else if(savefiles<100) filename+="_0"+savefiles.ToString(); else filename+="_"+savefiles.ToString(); int leg=filepath.Length; filename=filepath.Substring(0,leg-4)+filename+filepath.Substring(leg-4,4); this.pictureBox1.Image.Save(filename); this.t_out.Text="保存图片“"+filename+"”成功!"; if(savefiles%2==0) this.t_out.ForeColor=Color.Red; else this.t_out.ForeColor=Color.Blue; savefiles++; } } }
C# 如何采集摄像头的视频
最新推荐文章于 2016-01-21 16:15:37 发布