下面的这段代码,在昨晚运行的时候还能实现除录像外的其他基本功能,但今晚运行时,初始化加载获取画面就不能获取了,弄了很久还是找不到原因,真的很烦,哪位大侠能帮帮忙,小弟不胜感激
//程序入口文件Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace webcam
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
           // Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2());
        }
    }
}

解决方案 »

  1.   

    ------------------------------------------------------------------------
    //主要代码文件Form2.cs
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.Runtime.InteropServices;namespace webcam
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
          
            const short WM_CAP = 1024; 
            const int WM_CAP_DRIVER_CONNECT = WM_CAP + 10; 
            const int WM_CAP_DRIVER_DISCONNECT = WM_CAP + 11;
            const int WM_CAP_STOP = WM_CAP + 68;
            const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP + 20;
            const int WM_CAP_SEQUENCE_NOFILE = WM_CAP + 63;
            const int WM_CAP_SEQUENCE = WM_CAP + 62;
            const int WM_CAP_FILE_GET_CAPTURE_FILE = WM_CAP + 21; //获得捕获文件
            const int WM_CAP_SAVEDIB = WM_CAP + 25;//保存文件
            const int WM_CAP_EDIT_COPY = WM_CAP + 30;
            const int WM_CAP_SET_PREVIEW = WM_CAP + 50; 
            const int WM_CAP_SET_PREVIEWRATE = WM_CAP + 52; 
            const int WM_CAP_SET_SCALE = WM_CAP + 53; 
            const int WS_CHILD = 1073741824; 
            const int WS_VISIBLE = 268435456; 
            const short SWP_NOMOVE = 2; 
            const short SWP_NOSIZE = 1; 
            const short SWP_NOZORDER = 4; 
            const short HWND_BOTTOM = 1; 
            int iDevice = 0; //什么参数
            int hHwnd;        //////////////////////////////////////////////////////////////////////////////////////////////////////
            //[System.Runtime.InteropServices.DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            //static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID); 
            //////////////////////////////////////////////////////////////////////////////////////////////////////
            [System.Runtime.InteropServices.DllImport("user32", EntryPoint="SendMessageA", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
            static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] 
                object lParam); 
            [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetWindowPos")] 
            static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); 
            [System.Runtime.InteropServices.DllImport("user32")] 
            static extern bool DestroyWindow(int hndw);
            ///////////////////////////////////////
           // [System.Runtime.InteropServices.DllImport("User32.dll")]
            //static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam);        /////////////////////////////////////////////////////////
            [System.Runtime.InteropServices.DllImport("avicap32.dll")] 
            static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID); 
            [System.Runtime.InteropServices.DllImport("avicap32.dll")] 
            static extern bool capGetDriverDescriptionA(short wDriver, string lpszName, int cbName, string lpszVer, int cbVer); 
           
            private void OpenPreviewWindow() 
            {
                int iHeight = this.Size.Height;
                int iWidth =this.Size.Width; 
                // 
                //  Open Preview window in picturebox 
                // 
                hHwnd = capCreateCaptureWindowA(iDevice.ToString(), (WS_VISIBLE | WS_CHILD), 0, 0, 640, 480, picCapture.Handle.ToInt32(), 0); 
                // 
                //  Connect to device 
                // 
                if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) == 1) 
                { 
                    // 
                    // Set the preview scale 
                    // 
                    SendMessage(hHwnd, WM_CAP_SET_SCALE, 1, 0); 
                    // 
                    // Set the preview rate in milliseconds 
                    // 
                    SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0); 
                    // 
                    // Start previewing the image from the camera 
                    // 
                    SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, 0); 
                    // 
                    //  Resize window to fit in picturebox 
                    // 
                    SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, iWidth, iHeight, (SWP_NOMOVE | SWP_NOZORDER)); 
                } 
                else 
                { 
                    // 
                    //  Error connecting to device close window 
                    //  
                    DestroyWindow(hHwnd); 
                } 
            } 
            private void ClosePreviewWindow() 
            { 
                // 
                //  Disconnect from device 
                // 
                SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0); 
                // 
                //  close window 
                // 
               // DestroyWindow(hHwnd); 
            }
            //截图(抓图)
            //private void GrabImage(string path)
            //{
            //    IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
            //    SendMessage(hHwnd, WM_CAP_SAVEDIB, 0, hBmp.ToInt64());
            //}      
            //初始化
            private void Form2_Load(object sender, EventArgs e)
            {
                OpenPreviewWindow();
                this.button1.Enabled = false;
                this.button2.Enabled = true;
                this.button3.Enabled = true;
                this.button4.Enabled = true;
                this.button5.Enabled = true;
                this.button6.Enabled = false;
                device_number_textBox.Select();
                
            }
            //播放
            private void button1_Click_1(object sender, EventArgs e)
            {
                OpenPreviewWindow();
                this.button1.Enabled = false;
                this.button2.Enabled = true;
                this.button3.Enabled = true;
                this.button4.Enabled = true;
                this.button5.Enabled = true;
                this.button6.Enabled = false;        }
      

  2.   

            //停止
            private void button2_Click(object sender, EventArgs e)
            {
                ClosePreviewWindow();
                this.button1.Enabled = true;
                this.button2.Enabled = false;
                this.button3.Enabled = false;
                this.button4.Enabled = false;
                this.button5.Enabled = false;
                this.button6.Enabled = false;
            }
            //暂停 (暂停功能也不能很好的实现,望一块解决)我的暂停思想是:先在断开连接并截图,把截的图读到//picturebox里面。,这里我还是用一个panel,不怕麻烦可以在暂停时隐藏panel
            private void button5_Click(object sender, EventArgs e)
            {
                //if (MyVideo != null)
                //{
                //    MyVideo.Pause();
                //}
                SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0);
                pictureBox1.Height = this.Size.Height;
                pictureBox1.Width = this.Size.Width;
                Bitmap flag = new Bitmap("路径\\http_imgload.jpg");
                Graphics flagGraphics = Graphics.FromImage(flag);
                //this.pictureBox1.Image = (System.Drawing.Image)pic;
                pictureBox1.Image =flag ;
                this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                //this.picCapture.Visible = false;
                this.button1.Enabled = true;
                this.button2.Enabled = false;
                this.button3.Enabled = false;
                this.button4.Enabled = true;
                this.button5.Enabled = false;
                this.button6.Enabled = false;        }
            //截图、抓图
            private void button4_Click(object sender, System.EventArgs e)
            {
                try
                {
                    //SendMessage(hHwnd, WM_CAP_SAVEDIB, 0, 0);
                    SendMessage(this.hHwnd, 0x41e, 0, 0);
                    IDataObject obj1 = Clipboard.GetDataObject();
                    if (obj1.GetDataPresent(DataFormats.Bitmap))//typeof(Bitmap)
                    {
                        Image image1 = (Image)obj1.GetData(typeof(Bitmap));
                        // this.panel_Vedio.Image = image1;
                        // this.ClosePreviewWindow();                    SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
                        SaveFileDialog1.FileName = "img" + DateTime.Now.ToString("yyyyMMddHHmmssfff");//yyyyMMddHHmmssfff
                        SaveFileDialog1.Filter = "jpg|*.jpg|bmp|*.bmp|gif|*.gif";
                        //SaveFileDialog1.Filter = "Image Files(*.JPG;*.GIF)|*.JPG;*.GIF|All files (*.*)|*.*";
                        if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            image1.Save(SaveFileDialog1.FileName, ImageFormat.Bmp);
                        }
                        this.button1.Enabled = true;
                        this.button2.Enabled = true;
                        this.button3.Enabled = true;
                        this.button4.Enabled = true;
                        this.button5.Enabled = true;
                        this.button6.Enabled = true;
                        device_number_textBox.Text = "截图成功";
                    }
                    else
                    {
                        device_number_textBox.Text = obj1.GetDataPresent(typeof(Bitmap)).ToString();
                        device_number_textBox.Text += "error1";
                    }
                }
                catch
                {
                    device_number_textBox.Text = "error2";
                }//本文转摘自『蓝派网』http://www.lan27.com/Article/200706/1750.htm
            }
            //录像
            private void button3_Click(object sender, EventArgs e)
            {
                Kinescope(@"e:\media\"+DateTime.Now.ToString("yyyyMMddHHmmssfff")+".avi");
                this.button1.Enabled = true;
                this.button2.Enabled = true;
                this.button3.Enabled = true;
                this.button4.Enabled = true;
                this.button5.Enabled = false;
                this.button6.Enabled = true;
            }
            /// <summary>
            /// 录像
            /// </summary>
            /// <param name="path">要保存avi文件的路径</param>
            public void Kinescope(string path)
            {
                IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
               // SendMessage(this.hHwnd, 0x41e, 0, 0);
                SendMessage(this.hHwnd, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp);
                SendMessage(this.hHwnd, WM_CAP_SEQUENCE, 0, 0);
            }
            /// <summary>
            /// 停止录像
            /// </summary>
            public void StopKinescope()
            {
                SendMessage(this.hHwnd, WM_CAP_STOP, 0, 0);
            }
            //停止录像
            private void button6_Click(object sender, EventArgs e)
            {
                StopKinescope();
                this.button1.Enabled = true;
                this.button2.Enabled = true;
                this.button3.Enabled = true;
                this.button4.Enabled = true;
                this.button5.Enabled = true;
                this.button6.Enabled = false;
            }
            }    } 
      

  3.   

    -------------------------------------------------------------------------------------------
    //  Form2.Designer.cs文件
    namespace webcam
    {
        partial class Form2
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form2));
                this.button1 = new System.Windows.Forms.Button();
                this.device_number_textBox = new System.Windows.Forms.TextBox();
                this.picCapture = new System.Windows.Forms.Panel();
                this.pictureBox1 = new System.Windows.Forms.PictureBox();
                this.button2 = new System.Windows.Forms.Button();
                this.button3 = new System.Windows.Forms.Button();
                this.button4 = new System.Windows.Forms.Button();
                this.button5 = new System.Windows.Forms.Button();
                this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
                this.button6 = new System.Windows.Forms.Button();
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(12, 12);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "开始";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click_1);
                // 
                // device_number_textBox
                // 
                this.device_number_textBox.Location = new System.Drawing.Point(505, 14);
                this.device_number_textBox.Name = "device_number_textBox";
                this.device_number_textBox.Size = new System.Drawing.Size(75, 21);
                this.device_number_textBox.TabIndex = 1;
                this.device_number_textBox.Text = "不知啥意思";
                // 
                // picCapture
                // 
                this.picCapture.Location = new System.Drawing.Point(12, 41);
                this.picCapture.Name = "picCapture";
                this.picCapture.Size = new System.Drawing.Size(481, 350);
                this.picCapture.TabIndex = 2;
                // 
                // pictureBox1
                // 
                //this.pictureBox1.Image = image;没添加图片
                this.pictureBox1.Location = new System.Drawing.Point(515, 41);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(311, 350);
                this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                this.pictureBox1.TabIndex = 7;
                this.pictureBox1.TabStop = false;
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(93, 12);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(75, 23);
                this.button2.TabIndex = 3;
                this.button2.Text = "停止";
                this.button2.UseVisualStyleBackColor = true;
                this.button2.Click += new System.EventHandler(this.button2_Click);
                // 
                // button3
                // 
                this.button3.Location = new System.Drawing.Point(336, 12);
                this.button3.Name = "button3";
                this.button3.Size = new System.Drawing.Size(75, 23);
                this.button3.TabIndex = 4;
                this.button3.Text = "录像";
                this.button3.UseVisualStyleBackColor = true;
                this.button3.Click += new System.EventHandler(this.button3_Click);
                // 
                // button4
                // 
                this.button4.Location = new System.Drawing.Point(255, 12);
                this.button4.Name = "button4";
                this.button4.Size = new System.Drawing.Size(75, 23);
                this.button4.TabIndex = 5;
                this.button4.Text = "截图";
                this.button4.UseVisualStyleBackColor = true;
                this.button4.Click += new System.EventHandler(this.button4_Click);
                // 
                // button5
                // 
                this.button5.Location = new System.Drawing.Point(174, 12);
                this.button5.Name = "button5";
                this.button5.Size = new System.Drawing.Size(75, 23);
                this.button5.TabIndex = 6;
                this.button5.Text = "暂停";
                this.button5.UseVisualStyleBackColor = true;
                this.button5.Click += new System.EventHandler(this.button5_Click);
                // 
                // button6
                // 
                this.button6.Location = new System.Drawing.Point(417, 12);
                this.button6.Name = "button6";
                this.button6.Size = new System.Drawing.Size(75, 23);
                this.button6.TabIndex = 8;
                this.button6.Text = "停止录像";
                this.button6.UseVisualStyleBackColor = true;
                this.button6.Click += new System.EventHandler(this.button6_Click);
                // 
                // Form2
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(838, 407);
                this.Controls.Add(this.button6);
                this.Controls.Add(this.pictureBox1);
                this.Controls.Add(this.button3);
                this.Controls.Add(this.button4);
                this.Controls.Add(this.button5);
                this.Controls.Add(this.button2);
                this.Controls.Add(this.picCapture);
                this.Controls.Add(this.device_number_textBox);
                this.Controls.Add(this.button1);
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "Form2";
                this.Text = "GoodView test Web Camera";
                this.Load += new System.EventHandler(this.Form2_Load);
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();        }        #endregion        private System.Windows.Forms.Button button1;
            private System.Windows.Forms.TextBox device_number_textBox;
            private System.Windows.Forms.Panel picCapture;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.Button button3;
            private System.Windows.Forms.Button button4;
            private System.Windows.Forms.Button button5;
            private System.Windows.Forms.SaveFileDialog saveFileDialog1;
            private System.Windows.Forms.PictureBox pictureBox1;
            private System.Windows.Forms.Button button6;
        }
    }