Advertisement
Rythorian

GoCam

Feb 20th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | Source Code | 0 0
  1. Imports System.IO
  2. Imports AForge.Video.DirectShow
  3. Imports System.Timers
  4. Imports AForge.Video
  5. Public Class Form1
  6.  
  7. Private videoSource As VideoCaptureDevice
  8. Private imageCounter As Integer = 0
  9. Private savePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "WebCamImages")
  10. Private captureTimer As Timer
  11.  
  12. Public Sub New()
  13. InitializeCaptureDirectory()
  14. InitializeVideoSource()
  15. ' This call is required by the designer.
  16. InitializeTimer()
  17.  
  18. ' Add any initialization after the InitializeComponent() call.
  19. End Sub
  20.  
  21. Private Sub InitializeCaptureDirectory()
  22. If Not Directory.Exists(savePath) Then
  23. Directory.CreateDirectory(savePath)
  24. End If
  25. End Sub
  26.  
  27. Private Sub InitializeVideoSource()
  28. Dim videoDevices As New FilterInfoCollection(FilterCategory.VideoInputDevice)
  29. If videoDevices.Count > 0 Then
  30. videoSource = New VideoCaptureDevice(videoDevices(0).MonikerString)
  31. AddHandler videoSource.NewFrame, AddressOf OnNewFrame
  32. videoSource.Start()
  33. End If
  34. End Sub
  35.  
  36. Private Sub InitializeTimer()
  37. captureTimer = New Timer(10000)
  38. AddHandler captureTimer.Elapsed, AddressOf OnTimerElapsed
  39. captureTimer.Start()
  40. End Sub
  41.  
  42. Private Sub OnNewFrame(sender As Object, eventArgs As NewFrameEventArgs)
  43. Dim image As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap)
  44. SaveImage(image)
  45. IncrementImageCounter()
  46.  
  47. If ShouldStopCapturing() Then
  48. StopVideoSource()
  49. End If
  50. End Sub
  51.  
  52. Private Sub OnTimerElapsed(source As Object, e As ElapsedEventArgs)
  53. If videoSource IsNot Nothing AndAlso videoSource.IsRunning Then
  54. videoSource.SignalToStop()
  55. videoSource.WaitForStop()
  56. videoSource.Start()
  57. End If
  58. End Sub
  59.  
  60.  
  61. Private Sub SaveImage(image As Bitmap)
  62. Dim fileName As String = Path.Combine(savePath, $"Image_{imageCounter}.jpg")
  63. image.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg)
  64. End Sub
  65.  
  66. Private Sub IncrementImageCounter()
  67. imageCounter += 1
  68. End Sub
  69.  
  70. Private Function ShouldStopCapturing() As Boolean
  71. Return imageCounter >= 2 'this is how many pics are taken
  72. End Function
  73.  
  74.  
  75. Private Sub StopVideoSource()
  76. videoSource.SignalToStop()
  77. videoSource.WaitForStop()
  78. End Sub
  79. End Class
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement