C # using Aforge.net components to operate the camera

Source: Internet
Author: User

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?
    1. Private void Video_newframe (object sender, Newframeeventargs eventArgs)
    2. {
    3. Bitmap img = (Bitmap) eventArgs.Frame.Clone ();
    4. //do processing here
    5. pictureBox1.Image = img;
    6. }

Start by referencing this code to write:

[CSharp]View Plaincopyprint?
    1. private void  Video_newframe (object sender, newframeeventargs eventargs)   
    2.        {  
    3.            Bitmap img =  (Bitmap) EventArgs.Frame.Clone ();   
    4.             string imgpath = dirc +  "/"  +  datetime.now.tostring ( "Yyyymmddhhmmss")  +  ". jpg";   
    5.             img. Save (Imgpath);   
    6.        }  

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?
  1. private int flag = 1;
  2. <summary>
  3. ///   
  4. // </summary>
  5. private void Toolstripbutton3_click (object sender, EventArgs e)
  6. {
  7. Flag = 0;
  8. Videosource.newframe + = new Newframeeventhandler (Video_newframe);
  9. }
  10. private void Video_newframe (object sender, Newframeeventargs eventArgs)
  11. {
  12. Bitmap Bitmap = (Bitmap) eventArgs.Frame.Clone ();
  13. if (flag = = 0)
  14. {
  15. string img = Dirc + "/" + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". jpg";
  16. Bitmap. Save (IMG);
  17. flag = 1;
  18. }
  19. }

The complete program code is as follows:

[CSharp]View Plaincopyprint?
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.ComponentModel;
  4. Using System.Data;
  5. Using System.Drawing;
  6. Using System.Drawing.Imaging;
  7. Using System.Text;
  8. Using System.Windows.Forms;
  9. Using System.IO;
  10. Using Aforge;
  11. Using Aforge.video;
  12. Using AForge.Video.DirectShow;
  13. Using Aforge.imaging;
  14. Using AForge.Imaging.Filters;
  15. Namespace Camera
  16. {
  17. Public partial class Form1:form
  18. {
  19. private filterinfocollection videodevices;
  20. private Videocapturedevice VideoSource;
  21. private int flag = 1;
  22. private String dirc = System.AppDomain.CurrentDomain.BaseDirectory + "Images"; //Saved directories
  23. Public Form1 ()
  24. {
  25. InitializeComponent ();
  26. }
  27. private void Form1_Load (object sender, EventArgs e)
  28. {
  29. if (! Directory.Exists (DIRC))
  30. Directory.CreateDirectory (DIRC);
  31. Try
  32. {
  33. //Enumerate all video input devices
  34. Videodevices = new Filterinfocollection (Filtercategory.videoinputdevice);
  35. if (Videodevices.count = = 0)
  36. throw new ApplicationException ();
  37. foreach (FilterInfo device in videodevices)
  38. {
  39. TSCBXCAMERAS.ITEMS.ADD (device. Name);
  40. }
  41. Tscbxcameras.selectedindex = 0;
  42. }
  43. catch (ApplicationException)
  44. {
  45. TSCBXCAMERAS.ITEMS.ADD ("No Local capture Devices");
  46. Videodevices = null;
  47. }
  48. }
  49. private void Toolstripbutton1_click (object sender, EventArgs e)
  50. {
  51. Cameraconn ();
  52. }
  53. // <summary>
  54. /// connect to turn on camera
  55. // </summary>
  56. private void Cameraconn ()
  57. {
  58. VideoSource = new Videocapturedevice (Videodevices[tscbxcameras.selectedindex].  monikerstring);
  59. Videosource.desiredframesize = New Size (500, 300);
  60. Videosource.desiredframerate = 1;
  61. Videplayer.videosource = VideoSource;
  62. Videplayer.start ();
  63. }
  64. // <summary>
  65. /// Turn off the camera
  66. // </summary>
  67. private void Toolstripbutton2_click (object sender, EventArgs e)
  68. {
  69. Videplayer.signaltostop ();
  70. Videplayer.waitforstop ();
  71. }
  72. private void form1_formclosing (object sender, FormClosingEventArgs e)
  73. {
  74. Toolstripbutton2_click (null, null);
  75. }
  76. // <summary>
  77. ///   
  78. // </summary>
  79. private void Toolstripbutton3_click (object sender, EventArgs e)
  80. {
  81. Flag = 0;
  82. Videosource.newframe + = new Newframeeventhandler (Video_newframe);
  83. }
  84. private void Video_newframe (object sender, Newframeeventargs eventArgs)
  85. {
  86. Bitmap Bitmap = (Bitmap) eventArgs.Frame.Clone ();
  87. if (flag = = 0)
  88. {
  89. string img = Dirc + "/" + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". jpg";
  90. Bitmap. Save (IMG);
  91. flag = 1;
  92. }
  93. }
  94. }
  95. }

The interface effect is as follows:

C # using Aforge.net components to operate the camera

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.