AForgeを使用してUSB接続カメラから画像を取得して画面表示する方法を記載します。
USB接続カメラから画像を取得して表示方法として、AForgeを使用すると簡単に出来ます。
接続にはDirectShowを使用しますので、
・AForge.Video
・AForge.Video.DirectShow
をインポートします。
USB接続カメラの画像を表示
フォームにボタン2つ pictureBoxを1つ 配置します。
Dim _videoSource As VideoCaptureDevice = Nothing
'ビデオデバイスの起動
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
'ビデオ入力デバイスのみ取得
Dim videoDevices = New FilterInfoCollection(FilterCategory.VideoInputDevice)
If videoDevices.Count = 0 Then 'ビデオデバイスが無い
Return
End If
Dim MonikerString = videoDevices(0).MonikerString '最初のビデオデバイスを使用
_videoSource = New VideoCaptureDevice(MonikerString)
AddHandler _videoSource.NewFrame, AddressOf Me.Video_NewFrame
_videoSource.Start()
End Sub
'ビデオデバイスの停止
Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click
If _videoSource Is Nothing Then
Return
End If
If _videoSource.IsRunning Then
_videoSource.SignalToStop() 'ビデオデバイスの停止
_videoSource.WaitForStop() '完全に停止するまで待つ
_videoSource = Nothing
End If
End Sub
'ビデオデバイス取得画像表示
Private Sub Video_NewFrame(sender As Object, eventArgs As NewFrameEventArgs)
Dim img = DirectCast(eventArgs.Frame.Clone(), Bitmap)
PictureBox1.Image = img
End Sub