C # Use the AForge. NET component to operate the camera

Source: Internet
Author: User

AForge. NET is designed for developers and researchers based on the C # framework. This framework provides different class libraries and resources for class libraries. There are also many application examples, including computer vision and artificial intelligence, image processing, neural networks, genetic algorithms, machine learning, robotics, and other fields. According to the method on the Internet, the program camera came out, but what happened? Find an image display method [csharp] private void video_NewFrame (object sender, NewFrameEventArgs eventArgs) {Bitmap img = (Bitmap) eventArgs. frame. clone (); // do processing here pictureBox1.Image = img;}: [csharp] private void video_NewFrame (object sender, NewFrameEventArgs eventArgs) {Bitmap img = (Bitmap) eventArgs. frame. clone (); string imgPath = dirc + "/" + DateTime. now. toString ("yyyyMMddhhmmss") + ". Jpg"; img. Save (imgPath);} But clicking the program automatically captures a large number of images, and finally causes program problems. After thinking about a stupid method, you can cut the image only once: [csharp] private int flag = 1; /// <summary> ///// </summary> private void toolStripButton3_Click (object sender, EventArgs e) {flag = 0; videoSource. newFrame + = new NewFrameEventHandler (video_NewFrame);} private void video_NewFrame (object sender, NewFrameEventArgs eventArgs) {Bitmap bitmap = (Bitmap) eventArgs. frame. clone (); if (flag = 0) {string img = dirc + "/" + DateTime. N Ow. toString ("yyyyMMddhhmmss") + ". jpg "; bitmap. save (img); flag = 1 ;}} the complete program code is as follows: [csharp] using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. drawing. imaging; using System. text; using System. windows. forms; using System. IO; using AForge. video; using AForge. video. directShow; using AForge. imaging; using AFo Rge. imaging. filters; namespace Camera {public partial class Form1: Form {private FilterInfoCollection videoDevices; private VideoCaptureDevice videoSource; private int flag = 1; private string dirc = System. appDomain. currentDomain. baseDirectory + "Images"; // saved directory public Form1 () {InitializeComponent ();} private void Form1_Load (object sender, EventArgs e) {if (! Directory. exists (dirc) Directory. createDirectory (dirc); try {// enumerate all video input devices videoDevices = new FilterInfoCollection (FilterCategory. videoInputDevice); if (videoDevices. count = 0) throw new ApplicationException (); foreach (FilterInfo device in videoDevices) {tscbxCameras. items. add (device. name);} tscbxCameras. selectedIndex = 0;} catch (ApplicationException) {tscbxCameras. items. add ("No local capture devices"); videoDevices = null ;}} private void toolstripbutton#click (object sender, EventArgs e) {CameraConn ();} /// <summary> // enable camera connection /// </summary> private void CameraConn () {videoSource = new VideoCaptureDevice (videoDevices [tscbxCameras. selectedIndex]. monikerString); videoSource. desiredFrameSize = new Size (500,300); videoSource. desiredFrameRate = 1; videPlayer. videoSource = videoSource; videPlayer. start () ;}/// <summary> /// disable the camera /// </summary> private void toolStripButton2_Click (object sender, EventArgs e) {videPlayer. signalToStop (); videPlayer. waitForStop ();} private void Form1_FormClosing (object sender, FormClosingEventArgs e) {toolStripButton2_Click (null, null );} /// <summary> ///// </summary> private void toolStripButton3_Click (object sender, EventArgs e) {flag = 0; videoSource. newFrame + = new NewFrameEventHandler (video_NewFrame);} private void video_NewFrame (object sender, NewFrameEventArgs eventArgs) {Bitmap bitmap = (Bitmap) eventArgs. frame. clone (); if (flag = 0) {string img = dirc + "/" + DateTime. now. toString ("yyyyMMddhhmmss") + ". jpg "; bitmap. the Save (img); flag = 1 ;}}} interface is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.