winform loaded AForge.dll in videoSourcePlayer.

The company recently doing face recognition, uses videoSourcePlayer but after the introduction of AForge.dll, the toolbox is not videoSourcePlayer, then you need to add a tab. Very simple steps to complete.

First Right toolbox ---> Add Tab

 

 

 Name casually on, I was called AForge, and then select the tab right choices

 

 

 Select dll, click OK 

 

 

 Click again to add to the tab

 

 

 OK.

Then use the control. The videoSourcePlayer drag in winform. Enter the code behind

 

 Private FilterInfoCollection videoDevices; 

// Get camera 

Private  void the Form1_Load ( Object SENDER, EventArgs E) 
{ 
the try 
{ 
// enumerate all of the video input device 
videoDevices = new new FilterInfoCollection (FilterCategory.VideoInputDevice); 

IF (videoDevices.Count == 0 )
 the throw  new new ApplicationException (); 

the foreach (FilterInfo Device in videoDevices) 
{ 
comboBox1.Items.Add (device.Name); 
} 
// drop-down box is used to replace the camera 
ComboBox1.SelectedIndex = 0 ; 

}
the catch (ApplicationException) 
{ 
comboBox1.Items.Add ( " No Camera " ); 
ComboBox1.SelectedIndex = 0 ; 
videoDevices = null ; 
} 
}

 

 //关闭摄像头
        private void button2_Click(object sender, EventArgs e)
        {
            if (videoSourcePlayer != null && videoSourcePlayer.IsRunning)
            {
                videoSourcePlayer.SignalToStop();
                videoSourcePlayer.WaitForStop();
            }

        }
        //打开摄像头
        private void button1_Click(object sender, EventArgs e)
        {
            button2_Click(null, null);
            if (comboBox1.SelectedItem.ToString() == " No Camera " ) 
            { 
                MessageBox.Show ( " No Camera " , " error " , MessageBoxButtons.OK, MessageBoxIcon.Hand);
                 return ; 
            } 

            VideoCaptureDevice VideoSource = new new VideoCaptureDevice (videoDevices [ComboBox1.SelectedIndex] .MonikerString); 
            videoSource.VideoResolution = videoSource.VideoCapabilities [comboBox2.SelectedIndex]; 

            videoSourcePlayer.VideoSource = VideoSource; 
            videoSourcePlayer.Start (); 
        }

 // change the camera 
        Private  void comboBox1_SelectedIndexChanged ( Object SENDER, EventArgs E) 
        { 
            IF (comboBox1.SelectedItem.ToString () == " No Camera " ) 
            { 
                comboBox2.Items.Add ( " No Camera " ); 
                comboBox2.SelectedIndex = 0 ;
                 return ; 
            } 
            VideoCaptureDevice VideoSource = new new VideoCaptureDevice (videoDevices [ComboBox1.SelectedIndex] .MonikerString);
             IF (videoSource.VideoCapabilities.Count() == 0)
            {
                comboBox2.Items.Add("摄像头异常");
                comboBox2.SelectedIndex = 0;
                return;
            }
            comboBox2.Items.Clear();
            foreach (AForge.Video.DirectShow.VideoCapabilities FBL in videoSource.VideoCapabilities)
            {
                comboBox2.Items.Add(FBL.FrameSize.Width + "*" + FBL.FrameSize.Height);
            }

            comboBox2.SelectedIndex = 0;
            button1_Click(null, null);

        }

A total of just four method, first get the camera, then turn on the camera, turn off the camera, there are ways to obtain resolution of the replacement camera, so you can see the video playing in the camera and in the videoSourcePlayer

You need to get the current screen, call

Bitmap bitmap = videoSourcePlayer.GetCurrentVideoFrame();

Basically the method videoSourcePlayer in there, do not dwell on the.

Guess you like

Origin www.cnblogs.com/helloliu/p/11872311.html