C#.net 摄像头驱动程序,用avicap32.dll

时间:2022-02-25 19:28:37

装了摄像头后一般会有 avicap32.dll文件,没有一样可以用这些代码。

不需要在解决方案里面引用这个.dll文件。

下面有二种写法的例子:

例一:

C#.net 摄像头驱动程序,用avicap32.dllusing System;
C#.net 摄像头驱动程序,用avicap32.dllusing System.Runtime.InteropServices;
C#.net 摄像头驱动程序,用avicap32.dllusing System.Drawing;
C#.net 摄像头驱动程序,用avicap32.dllusing System.Drawing.Imaging;
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dllnamespace WinVideo
C#.net 摄像头驱动程序,用avicap32.dll{
C#.net 摄像头驱动程序,用avicap32.dll    /// <summary>
C#.net 摄像头驱动程序,用avicap32.dll    /// webcam 的摘要说明。
C#.net 摄像头驱动程序,用avicap32.dll    /// </summary>
C#.net 摄像头驱动程序,用avicap32.dll    public class webcam
C#.net 摄像头驱动程序,用avicap32.dll    {
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_USER=0x400;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WS_CHILD=0x40000000;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WS_VISIBLE=0x10000000;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_START=WM_USER;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_STOP=WM_CAP_START + 68;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_DRIVER_CONNECT=WM_CAP_START + 10;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_DRIVER_DISCONNECT=WM_CAP_START + 11;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_SAVEDIB=WM_CAP_START + 25;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_GRAB_FRAME=WM_CAP_START + 60;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_SEQUENCE=WM_CAP_START + 62;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_FILE_SET_CAPTURE_FILEA=WM_CAP_START + 20;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_SEQUENCE_NOFILE=WM_CAP_START+ 63;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_SET_OVERLAY=WM_CAP_START+ 51;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_SET_PREVIEW=WM_CAP_START+ 50;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM=WM_CAP_START +6;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_SET_CALLBACK_STATUSA=WM_CAP_START +3;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_SET_CALLBACK_FRAME=WM_CAP_START +5;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_SET_SCALE=WM_CAP_START+ 53;
C#.net 摄像头驱动程序,用avicap32.dll        private const int WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52;
C#.net 摄像头驱动程序,用avicap32.dll        private IntPtr hWndC;
C#.net 摄像头驱动程序,用avicap32.dll        private bool bStat = false;
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private IntPtr mControlPtr;
C#.net 摄像头驱动程序,用avicap32.dll        private int mWidth;
C#.net 摄像头驱动程序,用avicap32.dll        private int mHeight;
C#.net 摄像头驱动程序,用avicap32.dll        private int mLeft;
C#.net 摄像头驱动程序,用avicap32.dll        private int mTop;
C#.net 摄像头驱动程序,用avicap32.dll        private string GrabImagePath="";
C#.net 摄像头驱动程序,用avicap32.dll        private string KinescopePath="";
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        /// <summary>
C#.net 摄像头驱动程序,用avicap32.dll        /// 初始化摄像头
C#.net 摄像头驱动程序,用avicap32.dll        /// </summary>
C#.net 摄像头驱动程序,用avicap32.dll        /// <param name="handle">控件的句柄</param>
C#.net 摄像头驱动程序,用avicap32.dll        /// <param name="left">开始显示的左边距</param>
C#.net 摄像头驱动程序,用avicap32.dll        /// <param name="top">开始显示的上边距</param>
C#.net 摄像头驱动程序,用avicap32.dll        /// <param name="width">要显示的宽度</param>
C#.net 摄像头驱动程序,用avicap32.dll        /// <param name="height">要显示的长度</param>
C#.net 摄像头驱动程序,用avicap32.dll        public webcam(IntPtr handle,int left,int top,int width,int height)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            mControlPtr=handle;
C#.net 摄像头驱动程序,用avicap32.dll            mWidth=width;
C#.net 摄像头驱动程序,用avicap32.dll            mHeight=height;
C#.net 摄像头驱动程序,用avicap32.dll            mLeft=left;
C#.net 摄像头驱动程序,用avicap32.dll            mTop=top;
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll        "属性设置"
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        [DllImport("avicap32.dll")]
C#.net 摄像头驱动程序,用avicap32.dll        private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName,int dwStyle,int x,int y,int nWidth,int nHeight,IntPtr hWndParent,int nID);
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        [DllImport("avicap32.dll")]
C#.net 摄像头驱动程序,用avicap32.dll        private static extern int capGetVideoFormat(IntPtr hWnd,IntPtr psVideoFormat,int wSize );
C#.net 摄像头驱动程序,用avicap32.dll        [DllImport("User32.dll")]
C#.net 摄像头驱动程序,用avicap32.dll        private static extern bool SendMessage(IntPtr hWnd,int wMsg,int wParam,long lParam);
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        /// <summary>
C#.net 摄像头驱动程序,用avicap32.dll        /// 开始显示图像
C#.net 摄像头驱动程序,用avicap32.dll        /// </summary>
C#.net 摄像头驱动程序,用avicap32.dll        public void Start()
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            if(bStat)
C#.net 摄像头驱动程序,用avicap32.dll                return;
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll            bStat=true;
C#.net 摄像头驱动程序,用avicap32.dll            byte[] lpszName=new byte[100];
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll            hWndC=capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE ,mLeft,mTop,mWidth,mHeight,mControlPtr,0);
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll            if (hWndC.ToInt32()!=0)
C#.net 摄像头驱动程序,用avicap32.dll            {
C#.net 摄像头驱动程序,用avicap32.dll                SendMessage(hWndC,WM_CAP_SET_CALLBACK_VIDEOSTREAM,0,0);
C#.net 摄像头驱动程序,用avicap32.dll                SendMessage(hWndC,WM_CAP_SET_CALLBACK_ERROR,0,0);
C#.net 摄像头驱动程序,用avicap32.dll                SendMessage(hWndC,WM_CAP_SET_CALLBACK_STATUSA,0,0);
C#.net 摄像头驱动程序,用avicap32.dll                SendMessage(hWndC,WM_CAP_DRIVER_CONNECT,0,0);
C#.net 摄像头驱动程序,用avicap32.dll                SendMessage(hWndC,WM_CAP_SET_SCALE,1,0);
C#.net 摄像头驱动程序,用avicap32.dll                SendMessage(hWndC,WM_CAP_SET_PREVIEWRATE,66,0);
C#.net 摄像头驱动程序,用avicap32.dll                SendMessage(hWndC,WM_CAP_SET_OVERLAY,1,0);
C#.net 摄像头驱动程序,用avicap32.dll                SendMessage(hWndC,WM_CAP_SET_PREVIEW,1,0);
C#.net 摄像头驱动程序,用avicap32.dll            }
C#.net 摄像头驱动程序,用avicap32.dll            return;
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        /// <summary>
C#.net 摄像头驱动程序,用avicap32.dll        /// 停止显示
C#.net 摄像头驱动程序,用avicap32.dll        /// </summary>
C#.net 摄像头驱动程序,用avicap32.dll        public void Stop()
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            SendMessage(hWndC,WM_CAP_DRIVER_DISCONNECT,0,0);
C#.net 摄像头驱动程序,用avicap32.dll            bStat=false;
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        /// <summary>
C#.net 摄像头驱动程序,用avicap32.dll        /// 抓图
C#.net 摄像头驱动程序,用avicap32.dll        /// </summary>
C#.net 摄像头驱动程序,用avicap32.dll        /// <param name="path">要保存bmp文件的路径</param>
C#.net 摄像头驱动程序,用avicap32.dll        public void GrabImage()
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            IntPtr hBmp=Marshal.StringToHGlobalAnsi(GrabImagePath);
C#.net 摄像头驱动程序,用avicap32.dll            SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64());
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        /// <summary>
C#.net 摄像头驱动程序,用avicap32.dll        /// 录像
C#.net 摄像头驱动程序,用avicap32.dll        /// </summary>
C#.net 摄像头驱动程序,用avicap32.dll        /// <param name="path">要保存avi文件的路径</param>
C#.net 摄像头驱动程序,用avicap32.dll        public void Kinescope()
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            IntPtr hBmp=Marshal.StringToHGlobalAnsi(KinescopePath);
C#.net 摄像头驱动程序,用avicap32.dll            SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,0,hBmp.ToInt64());
C#.net 摄像头驱动程序,用avicap32.dll            SendMessage(hWndC,WM_CAP_SEQUENCE,0,0);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        /// <summary>
C#.net 摄像头驱动程序,用avicap32.dll        /// 停止录像
C#.net 摄像头驱动程序,用avicap32.dll        /// </summary>
C#.net 摄像头驱动程序,用avicap32.dll        public void StopKinescope()
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            SendMessage(hWndC,WM_CAP_STOP,0,0);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll    }
C#.net 摄像头驱动程序,用avicap32.dll}
C#.net 摄像头驱动程序,用avicap32.dll

对该类的调用:

C#.net 摄像头驱动程序,用avicap32.dll        public webcam wcam=null;
C#.net 摄像头驱动程序,用avicap32.dll        private void Form1_Load(object sender, System.EventArgs e)
C#.net 摄像头驱动程序,用avicap32.dll        {    
C#.net 摄像头驱动程序,用avicap32.dll            start();
C#.net 摄像头驱动程序,用avicap32.dll            this.btnKinescopeStop.Enabled=false;
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private void btnStar_Click(object sender, System.EventArgs e)
C#.net 摄像头驱动程序,用avicap32.dll        {        
C#.net 摄像头驱动程序,用avicap32.dll            wcam.Start();
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private void btnStop_Click(object sender, System.EventArgs e)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            wcam.Stop();
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private void btnSnapPic_Click(object sender, System.EventArgs e)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            //得到路径。例:d:/a.bmp
C#.net 摄像头驱动程序,用avicap32.dll            string myPath=this.txtPath.Text;
C#.net 摄像头驱动程序,用avicap32.dll            if(myPath=="")
C#.net 摄像头驱动程序,用avicap32.dll            {
C#.net 摄像头驱动程序,用avicap32.dll                MessageBox.Show("必须填写路径!");
C#.net 摄像头驱动程序,用avicap32.dll                return;
C#.net 摄像头驱动程序,用avicap32.dll            }
C#.net 摄像头驱动程序,用avicap32.dll            else
C#.net 摄像头驱动程序,用avicap32.dll            {
C#.net 摄像头驱动程序,用avicap32.dll                wcam.grabImagePath=myPath;
C#.net 摄像头驱动程序,用avicap32.dll                wcam.GrabImage();
C#.net 摄像头驱动程序,用avicap32.dll                MessageBox.Show("截图成功!");
C#.net 摄像头驱动程序,用avicap32.dll            }
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll        public void start()
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            //以panel1为容器显示视频内容
C#.net 摄像头驱动程序,用avicap32.dll            wcam=new webcam(panel1.Handle,0,0,this.panel1.Width,this.panel1.Height);        
C#.net 摄像头驱动程序,用avicap32.dll            wcam.Start();
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private void panel1_SizeChanged(object sender, System.EventArgs e)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            wcam.Stop();
C#.net 摄像头驱动程序,用avicap32.dll            wcam.Height=this.panel1.Height;
C#.net 摄像头驱动程序,用avicap32.dll            wcam.Width=this.panel1.Width;
C#.net 摄像头驱动程序,用avicap32.dll            wcam.Start();
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll        
C#.net 摄像头驱动程序,用avicap32.dll        private delegate void delegateKinescope();
C#.net 摄像头驱动程序,用avicap32.dll        private void btnKinescopeBegin_Click(object sender, System.EventArgs e)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            //得到路径。例:d:/a.avi
C#.net 摄像头驱动程序,用avicap32.dll            string myPath=this.txtPath.Text;
C#.net 摄像头驱动程序,用avicap32.dll            if(myPath=="")
C#.net 摄像头驱动程序,用avicap32.dll            {
C#.net 摄像头驱动程序,用avicap32.dll                MessageBox.Show("必须填写路径!");
C#.net 摄像头驱动程序,用avicap32.dll                return;
C#.net 摄像头驱动程序,用avicap32.dll            }
C#.net 摄像头驱动程序,用avicap32.dll            else
C#.net 摄像头驱动程序,用avicap32.dll            {
C#.net 摄像头驱动程序,用avicap32.dll                wcam.kinescopePath=myPath;
C#.net 摄像头驱动程序,用avicap32.dll                delegateKinescope myK=new delegateKinescope(wcam.Kinescope);
C#.net 摄像头驱动程序,用avicap32.dll                Thread threadKinescope=new Thread(new ThreadStart(myK));
C#.net 摄像头驱动程序,用avicap32.dll                threadKinescope.Start();
C#.net 摄像头驱动程序,用avicap32.dll                this.btnKinescopeBegin.Enabled=false;
C#.net 摄像头驱动程序,用avicap32.dll                this.btnKinescopeStop.Enabled=true;
C#.net 摄像头驱动程序,用avicap32.dll            }        
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll        public void starKinescope()
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            delegateKinescope myK=new delegateKinescope(wcam.Kinescope);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private void btnKinescopeStop_Click(object sender, System.EventArgs e)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            wcam.StopKinescope();
C#.net 摄像头驱动程序,用avicap32.dll            this.btnKinescopeBegin.Enabled=true;
C#.net 摄像头驱动程序,用avicap32.dll            this.btnKinescopeStop.Enabled=false;            
C#.net 摄像头驱动程序,用avicap32.dll        }

例二:

C#.net 摄像头驱动程序,用avicap32.dllusing System;
C#.net 摄像头驱动程序,用avicap32.dllusing System.Runtime.InteropServices;
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dllnamespace webcam
C#.net 摄像头驱动程序,用avicap32.dll{
C#.net 摄像头驱动程序,用avicap32.dll    /**//// 
C#.net 摄像头驱动程序,用avicap32.dll    /// avicap 的摘要说明。
C#.net 摄像头驱动程序,用avicap32.dll    /// 
C#.net 摄像头驱动程序,用avicap32.dll    public class showVideo
C#.net 摄像头驱动程序,用avicap32.dll    {
C#.net 摄像头驱动程序,用avicap32.dll        // showVideo calls
C#.net 摄像头驱动程序,用avicap32.dll        [DllImport("avicap32.dll")] public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
C#.net 摄像头驱动程序,用avicap32.dll        [DllImport("avicap32.dll")] public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);
C#.net 摄像头驱动程序,用avicap32.dll        [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam); 
C#.net 摄像头驱动程序,用avicap32.dll        [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam); 
C#.net 摄像头驱动程序,用avicap32.dll        [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, FrameEventHandler lParam); 
C#.net 摄像头驱动程序,用avicap32.dll        [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref BITMAPINFO lParam);
C#.net 摄像头驱动程序,用avicap32.dll        [DllImport("User32.dll")] public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
C#.net 摄像头驱动程序,用avicap32.dll        [DllImport("avicap32.dll")]public static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize );
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        // Constants
C#.net 摄像头驱动程序,用avicap32.dll        public const int WM_USER = 0x400;
C#.net 摄像头驱动程序,用avicap32.dll        public const int WS_CHILD = 0x40000000;
C#.net 摄像头驱动程序,用avicap32.dll        public const int WS_VISIBLE = 0x10000000;
C#.net 摄像头驱动程序,用avicap32.dll        public const int SWP_NOMOVE = 0x2;
C#.net 摄像头驱动程序,用avicap32.dll        public const int SWP_NOZORDER = 0x4;
C#.net 摄像头驱动程序,用avicap32.dll        public const int WM_CAP_DRIVER_CONNECT = WM_USER + 10;
C#.net 摄像头驱动程序,用avicap32.dll        public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11;
C#.net 摄像头驱动程序,用avicap32.dll        public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER + 5;
C#.net 摄像头驱动程序,用avicap32.dll        public const int WM_CAP_SET_PREVIEW = WM_USER + 50;
C#.net 摄像头驱动程序,用avicap32.dll        public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
C#.net 摄像头驱动程序,用avicap32.dll        public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45;
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll        // Structures
C#.net 摄像头驱动程序,用avicap32.dll        [StructLayout(LayoutKind.Sequential)] public struct VIDEOHDR
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public int lpData;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public int dwBufferLength;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public int dwBytesUsed;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public int dwTimeCaptured;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public int dwUser;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public int dwFlags;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)] public int[] dwReserved;
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        [StructLayout(LayoutKind.Sequential)] public struct BITMAPINFOHEADER
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public Int32 biSize ;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public Int32 biWidth ;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public Int32 biHeight ;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I2)] public short biPlanes;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I2)] public short biBitCount ;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public Int32 biCompression;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public Int32 biSizeImage;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public Int32 biXPelsPerMeter;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public Int32 biYPelsPerMeter;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public Int32 biClrUsed;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.I4)] public Int32 biClrImportant;
C#.net 摄像头驱动程序,用avicap32.dll        } 
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        [StructLayout(LayoutKind.Sequential)] public struct BITMAPINFO
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.Struct, SizeConst=40)] public BITMAPINFOHEADER bmiHeader;
C#.net 摄像头驱动程序,用avicap32.dll            [MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)] public Int32[] bmiColors;
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll        public delegate void FrameEventHandler(IntPtr lwnd, IntPtr lpVHdr);
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll        // Public methods
C#.net 摄像头驱动程序,用avicap32.dll        public static object GetStructure(IntPtr ptr,ValueType structure)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            return Marshal.PtrToStructure(ptr,structure.GetType());
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll        public static object GetStructure(int ptr,ValueType structure)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            return GetStructure(new IntPtr(ptr),structure);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll        public static void Copy(IntPtr ptr,byte[] data)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            Marshal.Copy(ptr,data,0,data.Length);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll        public static void Copy(int ptr,byte[] data)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            Copy(new IntPtr(ptr),data);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll        public static int SizeOf(object structure)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            return Marshal.SizeOf(structure); 
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll    }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll    //Web Camera Class
C#.net 摄像头驱动程序,用avicap32.dll    public class WebCamera
C#.net 摄像头驱动程序,用avicap32.dll    {
C#.net 摄像头驱动程序,用avicap32.dll        // Constructur
C#.net 摄像头驱动程序,用avicap32.dll        public WebCamera(IntPtr handle, int width,int height)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            mControlPtr = handle;
C#.net 摄像头驱动程序,用avicap32.dll            mWidth = width;
C#.net 摄像头驱动程序,用avicap32.dll            mHeight = height;
C#.net 摄像头驱动程序,用avicap32.dll        } 
C#.net 摄像头驱动程序,用avicap32.dll        // delegate for frame callback
C#.net 摄像头驱动程序,用avicap32.dll        public delegate void RecievedFrameEventHandler(byte[] data);
C#.net 摄像头驱动程序,用avicap32.dll        public event RecievedFrameEventHandler RecievedFrame;
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll        private IntPtr lwndC; // Holds the unmanaged handle of the control
C#.net 摄像头驱动程序,用avicap32.dll        private IntPtr mControlPtr; // Holds the managed pointer of the control
C#.net 摄像头驱动程序,用avicap32.dll        private int mWidth;
C#.net 摄像头驱动程序,用avicap32.dll        private int mHeight;
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll        private showVideo.FrameEventHandler mFrameEventHandler; // Delegate instance for the frame callback - must keep alive! gc should NOT collect it
C#.net 摄像头驱动程序,用avicap32.dll        // Close the web camera
C#.net 摄像头驱动程序,用avicap32.dll        public void CloseWebcam()
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            this.capDriverDisconnect(this.lwndC);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll        // start the web camera
C#.net 摄像头驱动程序,用avicap32.dll        public void StartWebCam()
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            byte[] lpszName = new byte[100];
C#.net 摄像头驱动程序,用avicap32.dll            byte[] lpszVer = new byte[100];
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll            showVideo.capGetDriverDescriptionA(0, lpszName, 100,lpszVer, 100);
C#.net 摄像头驱动程序,用avicap32.dll            this.lwndC = showVideo.capCreateCaptureWindowA(lpszName, showVideo.WS_VISIBLE + showVideo.WS_CHILD, 0, 0, mWidth, mHeight, mControlPtr, 0);
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll            if (this.capDriverConnect(this.lwndC, 0))
C#.net 摄像头驱动程序,用avicap32.dll            {
C#.net 摄像头驱动程序,用avicap32.dll                this.capPreviewRate(this.lwndC, 66);
C#.net 摄像头驱动程序,用avicap32.dll                this.capPreview(this.lwndC, true);
C#.net 摄像头驱动程序,用avicap32.dll                showVideo.BITMAPINFO bitmapinfo = new showVideo.BITMAPINFO(); 
C#.net 摄像头驱动程序,用avicap32.dll                bitmapinfo.bmiHeader.biSize = showVideo.SizeOf(bitmapinfo.bmiHeader);
C#.net 摄像头驱动程序,用avicap32.dll                bitmapinfo.bmiHeader.biWidth = 352;
C#.net 摄像头驱动程序,用avicap32.dll                bitmapinfo.bmiHeader.biHeight = 288;
C#.net 摄像头驱动程序,用avicap32.dll                bitmapinfo.bmiHeader.biPlanes = 1;
C#.net 摄像头驱动程序,用avicap32.dll                bitmapinfo.bmiHeader.biBitCount = 24;
C#.net 摄像头驱动程序,用avicap32.dll                this.capSetVideoFormat(this.lwndC, ref bitmapinfo, showVideo.SizeOf(bitmapinfo));
C#.net 摄像头驱动程序,用avicap32.dll                this.mFrameEventHandler = new showVideo.FrameEventHandler(FrameCallBack);
C#.net 摄像头驱动程序,用avicap32.dll                this.capSetCallbackOnFrame(this.lwndC, this.mFrameEventHandler);
C#.net 摄像头驱动程序,用avicap32.dll                showVideo.SetWindowPos(this.lwndC, 0, 0, 0, mWidth , mHeight , 6);
C#.net 摄像头驱动程序,用avicap32.dll            } 
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        // private functions
C#.net 摄像头驱动程序,用avicap32.dll        private bool capDriverConnect(IntPtr lwnd, short i)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            return showVideo.SendMessage(lwnd, showVideo.WM_CAP_DRIVER_CONNECT, i, 0);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll        private bool capDriverDisconnect(IntPtr lwnd)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            return showVideo.SendMessage(lwnd, showVideo.WM_CAP_DRIVER_DISCONNECT, 0, 0);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll        private bool capPreview(IntPtr lwnd, bool f)
C#.net 摄像头驱动程序,用avicap32.dll        {return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_PREVIEW , f, 0);}
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private bool capPreviewRate(IntPtr lwnd, short wMS)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_PREVIEWRATE, wMS, 0);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll 
C#.net 摄像头驱动程序,用avicap32.dll        private bool capSetCallbackOnFrame(IntPtr lwnd, showVideo.FrameEventHandler lpProc)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_CALLBACK_FRAME, 0, lpProc);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private bool capSetVideoFormat(IntPtr hCapWnd, ref showVideo.BITMAPINFO BmpFormat, int CapFormatSize)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            return showVideo.SendMessage(hCapWnd, showVideo.WM_CAP_SET_VIDEOFORMAT, CapFormatSize, ref BmpFormat);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private void FrameCallBack(IntPtr lwnd, IntPtr lpVHdr)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            showVideo.VIDEOHDR videoHeader = new showVideo.VIDEOHDR();
C#.net 摄像头驱动程序,用avicap32.dll            byte[] VideoData;
C#.net 摄像头驱动程序,用avicap32.dll            videoHeader = (showVideo.VIDEOHDR)showVideo.GetStructure(lpVHdr,videoHeader);
C#.net 摄像头驱动程序,用avicap32.dll            VideoData = new byte[videoHeader.dwBytesUsed];
C#.net 摄像头驱动程序,用avicap32.dll            showVideo.Copy(videoHeader.lpData ,VideoData);
C#.net 摄像头驱动程序,用avicap32.dll            if (this.RecievedFrame != null)
C#.net 摄像头驱动程序,用avicap32.dll                this.RecievedFrame (VideoData);
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll    }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll}
C#.net 摄像头驱动程序,用avicap32.dll

调用代码:

C#.net 摄像头驱动程序,用avicap32.dll        WebCamera wc;
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private void Form1_Load(object sender, System.EventArgs e)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            b_play.Enabled = false;
C#.net 摄像头驱动程序,用avicap32.dll            b_stop.Enabled = true;
C#.net 摄像头驱动程序,用avicap32.dll            panelPreview.Size = new Size(330,330);
C#.net 摄像头驱动程序,用avicap32.dll            wc = new WebCamera( panelPreview.Handle,panelPreview.Width,panelPreview.Height);
C#.net 摄像头驱动程序,用avicap32.dll            wc.StartWebCam();
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private void button1_Click(object sender, System.EventArgs e)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            b_play.Enabled = false;
C#.net 摄像头驱动程序,用avicap32.dll            b_stop.Enabled = true;
C#.net 摄像头驱动程序,用avicap32.dll            panelPreview.Size = new Size(330,330);
C#.net 摄像头驱动程序,用avicap32.dll            wc = new WebCamera( panelPreview.Handle,panelPreview.Width,panelPreview.Height);
C#.net 摄像头驱动程序,用avicap32.dll            wc.StartWebCam();
C#.net 摄像头驱动程序,用avicap32.dll        }
C#.net 摄像头驱动程序,用avicap32.dll
C#.net 摄像头驱动程序,用avicap32.dll        private void b_stop_Click(object sender, System.EventArgs e)
C#.net 摄像头驱动程序,用avicap32.dll        {
C#.net 摄像头驱动程序,用avicap32.dll            b_play.Enabled = true;
C#.net 摄像头驱动程序,用avicap32.dll            b_stop.Enabled = false;
C#.net 摄像头驱动程序,用avicap32.dll            wc.CloseWebcam();
C#.net 摄像头驱动程序,用avicap32.dll        }