Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Imports AForge.Video.DirectShow
- Imports System.Timers
- Imports AForge.Video
- Public Class Form1
- Private videoSource As VideoCaptureDevice
- Private imageCounter As Integer = 0
- Private savePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "WebCamImages")
- Private captureTimer As Timer
- Public Sub New()
- InitializeCaptureDirectory()
- InitializeVideoSource()
- ' This call is required by the designer.
- InitializeTimer()
- ' Add any initialization after the InitializeComponent() call.
- End Sub
- Private Sub InitializeCaptureDirectory()
- If Not Directory.Exists(savePath) Then
- Directory.CreateDirectory(savePath)
- End If
- End Sub
- Private Sub InitializeVideoSource()
- Dim videoDevices As New FilterInfoCollection(FilterCategory.VideoInputDevice)
- If videoDevices.Count > 0 Then
- videoSource = New VideoCaptureDevice(videoDevices(0).MonikerString)
- AddHandler videoSource.NewFrame, AddressOf OnNewFrame
- videoSource.Start()
- End If
- End Sub
- Private Sub InitializeTimer()
- captureTimer = New Timer(10000)
- AddHandler captureTimer.Elapsed, AddressOf OnTimerElapsed
- captureTimer.Start()
- End Sub
- Private Sub OnNewFrame(sender As Object, eventArgs As NewFrameEventArgs)
- Dim image As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap)
- SaveImage(image)
- IncrementImageCounter()
- If ShouldStopCapturing() Then
- StopVideoSource()
- End If
- End Sub
- Private Sub OnTimerElapsed(source As Object, e As ElapsedEventArgs)
- If videoSource IsNot Nothing AndAlso videoSource.IsRunning Then
- videoSource.SignalToStop()
- videoSource.WaitForStop()
- videoSource.Start()
- End If
- End Sub
- Private Sub SaveImage(image As Bitmap)
- Dim fileName As String = Path.Combine(savePath, $"Image_{imageCounter}.jpg")
- image.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg)
- End Sub
- Private Sub IncrementImageCounter()
- imageCounter += 1
- End Sub
- Private Function ShouldStopCapturing() As Boolean
- Return imageCounter >= 2 'this is how many pics are taken
- End Function
- Private Sub StopVideoSource()
- videoSource.SignalToStop()
- videoSource.WaitForStop()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement