Advertisement
Rythorian

GoCam

Feb 20th, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | Source Code | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Timers;
  5. using AForge.Video;
  6. using AForge.Video.DirectShow;
  7. using System.Windows.Forms;
  8.  
  9. public class Form1 : Form
  10. {
  11. private VideoCaptureDevice videoSource;
  12. private int imageCounter = 0;
  13. private string savePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "WebCamImages");
  14. private Timer captureTimer;
  15.  
  16. public Form1()
  17. {
  18. InitializeCaptureDirectory();
  19. InitializeVideoSource();
  20. InitializeTimer();
  21. }
  22.  
  23. private void InitializeCaptureDirectory()
  24. {
  25. if (!Directory.Exists(savePath))
  26. {
  27. Directory.CreateDirectory(savePath);
  28. }
  29. }
  30.  
  31. private void InitializeVideoSource()
  32. {
  33. var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  34. if (videoDevices.Count > 0)
  35. {
  36. videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
  37. videoSource.NewFrame += OnNewFrame;
  38. videoSource.Start();
  39. }
  40. }
  41.  
  42. private void InitializeTimer()
  43. {
  44. captureTimer = new Timer(10000);
  45. captureTimer.Elapsed += OnTimerElapsed;
  46. captureTimer.Start();
  47. }
  48.  
  49. private void OnNewFrame(object sender, NewFrameEventArgs eventArgs)
  50. {
  51. Bitmap image = (Bitmap)eventArgs.Frame.Clone();
  52. SaveImage(image);
  53. IncrementImageCounter();
  54.  
  55. if (ShouldStopCapturing())
  56. {
  57. StopVideoSource();
  58. }
  59. }
  60.  
  61. private void OnTimerElapsed(object source, ElapsedEventArgs e)
  62. {
  63. if (videoSource != null && videoSource.IsRunning)
  64. {
  65. videoSource.SignalToStop();
  66. videoSource.WaitForStop();
  67. videoSource.Start();
  68. }
  69. }
  70.  
  71. private void SaveImage(Bitmap image)
  72. {
  73. string fileName = Path.Combine(savePath, $"Image_{imageCounter}.jpg");
  74. image.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
  75. }
  76.  
  77. private void IncrementImageCounter()
  78. {
  79. imageCounter++;
  80. }
  81.  
  82. private bool ShouldStopCapturing()
  83. {
  84. return imageCounter >= 2; // this is how many pics are taken
  85. }
  86.  
  87. private void StopVideoSource()
  88. {
  89. videoSource.SignalToStop();
  90. videoSource.WaitForStop();
  91. }
  92. }
  93.  
  94. public static class Module1
  95. {
  96. [STAThread]
  97. public static void Main()
  98. {
  99. Application.Run(new Form1());
  100. }
  101. }
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement