Aforge.net is designed for developers and researchers based on the C # Framework, which provides different class libraries and resources for class libraries, as well as 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, running the program camera came out, but how? Find a way to display pictures as follows
[CSharp]View Plaincopyprint?
- Private void Video_newframe (object sender, Newframeeventargs eventArgs)
- {
- Bitmap img = (Bitmap) eventArgs.Frame.Clone ();
- //do processing here
- pictureBox1.Image = img;
- }
Start by referencing this code to write:
[CSharp]View Plaincopyprint?
- 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 click on the program automatically stop a lot of pictures, and finally cause the program problems. Think of a stupid method, you can only cut once the diagram:
[CSharp]View Plaincopyprint?
- 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.Now.ToString ("YYYYMMDDHHMMSS") + ". jpg";
- Bitmap. Save (IMG);
- flag = 1;
- }
- }
The complete program code is as follows:
[CSharp]View Plaincopyprint?
- 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;
- Using Aforge.video;
- Using AForge.Video.DirectShow;
- Using Aforge.imaging;
- Using AForge.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 directories
- 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 Toolstripbutton1_click (object sender, EventArgs e)
- {
- Cameraconn ();
- }
- // <summary>
- /// connect to turn on camera
- // </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>
- /// Turn off 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. Save (IMG);
- flag = 1;
- }
- }
- }
- }
The interface effect is as follows:
C # using Aforge.net components to operate the camera