copilotにサンプルプログラムを書いてもらいました
速攻で出来上がります。
そのままでは動かない場合もありますが、かなり楽にコードを書くことができます。
こちらに、VB.NETでAForge.Videoを使用してビデオキャプチャを行うサンプルコードがあります1。
Imports AForge.Video
Imports AForge.Video.DirectShow
Public Class Form1
Private videoSource As VideoCaptureDevice
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
Dim videoDevices As New FilterInfoCollection(FilterCategory.VideoInputDevice)
If videoDevices.Count = 0 Then
MessageBox.Show("ビデオデバイスが見つかりません。")
Return
End If
videoSource = New VideoCaptureDevice(videoDevices(0).MonikerString)
AddHandler videoSource.NewFrame, AddressOf video_NewFrame
videoSource.Start()
End Sub
Private Sub video_NewFrame(sender As Object, eventArgs As NewFrameEventArgs)
Dim bitmap As Bitmap = CType(eventArgs.Frame.Clone(), Bitmap)
PictureBox1.Image = bitmap
End Sub
Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click
If videoSource IsNot Nothing AndAlso videoSource.IsRunning Then
videoSource.SignalToStop()
videoSource.WaitForStop()
videoSource = Nothing
End If
End Sub
End Class
このコードでは、StartButton
がクリックされたときにビデオキャプチャを開始し、StopButton
がクリックされたときにビデオキャプチャを停止します。キャプチャされたフレームはPictureBox
に表示されます。
他に何かお手伝いできることがあれば教えてくださいね!
1: VB.NET AForge VideoSourcePlayerにて画像表示