AForgeを使用して、USB接続カメラの画像を動画保存する方法を記載します。
動画ファイルの作成には、AVIWriter クラスを使用します。
AForge.Video.VFWをインポートします。
AVI形式動画ファイル作成
Dim _aviWriter As AVIWriter = New AVIWriter() '"MSVC"などコーディック可能
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
Dim FilePath = "d:\test.avi"
Dim videoDevices = New FilterInfoCollection(FilterCategory.VideoInputDevice)
Dim videoSource = New VideoCaptureDevice(videoDevices(0).MonikerString) '最初に見つかったビデオデバイスを使用
AddHandler videoSource.NewFrame, AddressOf Me.Video_NewFrame
Dim videoCapabilities = videoSource.VideoCapabilities 'ビデオデバイスの提供する機能一覧を配列に格納
videoSource.VideoResolution = videoCapabilities(8) 'フレームサイズ設定
_aviWriter.FrameRate = videoSource.VideoResolution.FrameRate 'aviフレームレート設定
_aviWriter.Open(FilePath,
videoSource.VideoResolution.FrameSize.Width,
videoSource.VideoResolution.FrameSize.Height) 'aviファイルストリームを生成
videoSource.Start() 'ビデオデバイスの開始
For i = 0 To 10000
System.Threading.Thread.Sleep(1)
System.Windows.Forms.Application.DoEvents()
Next
videoSource.SignalToStop() 'ビデオデバイスの停止
videoSource.WaitForStop() '完全に停止するまで待つ
videoSource = Nothing
_aviWriter.Close()
End Sub
Private Sub Video_NewFrame(sender As Object, eventArgs As NewFrameEventArgs)
Dim img = DirectCast(eventArgs.Frame.Clone(), Bitmap)
_aviWriter.AddFrame(img) 'ファイル書き込み
PictureBox1.Image = img
End Sub
※New AVIWriter() の際、引数にcodecを指定することが出来ます。