IC Imaging Control .NET

Capturing an AVI File

Capturing an AVI File

This chapter shows you how to save a video stream as a compressed AVI file.

To see how to save video streams in different file formats, see the Capturing a Video File example.

The source code for the VB.NET and C# versions of this sample program can be found in the directories samples\VB *\Capturing an AVI File and samples\C# *\Capturing an AVI File in your My Documents/IC Imaging Control 3.4 directory.

Setting up the Project

Create a new project and add IC Imaging Control to the form. Before you run the program, select the video capture device, input and video format as shown in the FirstSteps chapter. Alternatively, run the program without selecting a device. In this case, the program shows the device selection dialog provided by IC Imaging Control. If you close this dialog without having first made a selection, the program will display an error message and terminate.

Now add 3 buttons to the form and label them Start Live, Stop Live and Capture AVI. Name the buttons cmdStartLive, cmdStopLive and cmdCaptureVideo respectively.

Add calls to IC Imaging Control's LiveStart and LiveStop methods:

[VB.NET]
Private Sub cmdStartLive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStartLive.Click IcImagingControl1.LiveStart() End Sub
[C#]
private void cmdStartLive_Click(object sender, EventArgs e) { icImagingControl1.LiveStart(); }
[VB.NET]
Private Sub cmdStopLive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStopLive.Click IcImagingControl1.LiveStop() End Sub
[C#]
private void cmdStopLive_Click(object sender, EventArgs e) { icImagingControl1.LiveStop(); }

Run the program. You can now start and stop the live image by clicking on the respective buttons. This does not yet create an AVI file, but is required to create a preview for the live video stream. This is usually required to set up the camera before actually starting to record.

image

The AVI Recording Dialog

Now add another form to your project. Name it frmWriteAVI. Add a combo box for the available video codecs, a text box for the AVI filename, and buttons to start and stop AVI capture. (You may want to load the sample project instead of creating the form manually).

image

In the FormLoad event, the selection list is filled with the names of all video codecs installed on your computer. These can be used as an AVI compressor.

[VB.NET]
Private Sub writeavi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Insert all installed codecs into the cboVideoCodec combobox. For Each Codec As TIS.Imaging.AviCompressor In ICControl.AviCompressors cboVideoCodec.Items.Add(Codec) Next ' Show the first codec in the combobox. cboVideoCodec.SelectedIndex = 0 End Sub
[C#]
private void WriteAvi_Load(object sender, EventArgs e) { cboVideoCodec.DataSource = ICControl.AviCompressors; // Show the first codec in the combobox. cboVideoCodec.SelectedIndex = 0; cmdStopCapture.Enabled = true; cmdStartCapture.Enabled = true; }

The Click event of the Start Capture button calls IC Imaging Control's AviStartCapture method, with the filename and the name of the video codec as a parameter. There is also some code to disable the Close button and enable the Stop Capture button and to check whether the user has selected a codec and a filename:

[VB.NET]
Private Sub cmdStartCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStartCapture.Click If txtFilename.Text = "" Then MsgBox("Please select an AVI filename first.", vbExclamation + vbOKOnly) Else ICControl.AviStartCapture(txtFilename.Text, cboVideoCodec.SelectedItem) cmdStopCapture.Enabled = True cmdStartCapture.Enabled = False End If End Sub
[C#]
private void cmdStartCapture_Click(object sender, EventArgs e) { if (txtFilename.Text == "") { MessageBox.Show("Please select an AVI filename first."); } else { ICControl.AviStartCapture(txtFilename.Text, cboVideoCodec.SelectedItem.ToString()); cmdStopCapture.Enabled = true; cmdStartCapture.Enabled = true; } }

The Pause button is a check box with a graphical style. It is used to set and reset the LiveCapturePause property of IC Imaging Control. If the LiveCapturePause property is set to True, AVI capture is paused. This means, that only the sink path of the image stream is paused. The live video is still displayed, but the video is not saved to the video file. If the LiveCapturePause property is set to False, IC Imaging Control immediately continues AVI capture. A previously started AVI file will be continued.

[VB.NET]
Private Sub chkPause_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPause.CheckedChanged ICControl.LiveCapturePause = chkPause.CheckState End Sub
[C#]
private void chkPause_CheckedChanged(object sender, EventArgs e) { ICControl.LiveCapturePause = chkPause.Checked; }

The AVI capture can be started by a call to AviStartCapture, even if the LiveCapturePause property is set to True. In this case, the image stream is initialized and started and the live video is displayed. The images are not saved to the AVI file because the sink path is paused. As soon as the LiveCapturePause property is set to False, the sink path is started and images are written to the AVI file. The advantage of this is that the AVI capturing can be started, paused and restarted immediately on an event.

Finished. You can now run the program, select a codec, and create an AVI file. Note that compressing a video stream is a highly CPU intensive operation and not all codecs will be able to work at full image resolution and full frame rate.

<< Programmer's Guide

Get in touch with us


About The Imaging Source

Established in 1990, The Imaging Source is one of the leading manufacturers of industrial cameras, frame grabbers and video converters for production automation, quality assurance, logistics, medicine, science and security.

Our comprehensive range of cameras with USB 3.1, USB 3.0, USB 2.0, GigE interfaces and other innovative machine vision products are renowned for their high quality and ability to meet the performance requirements of demanding applications.

Automated Imaging Association ISO 9001:2015 certified

Contact us