Displaying Buffers via FrameQueueSink

This chapter shows how to control the display of image buffers using a FrameQueueSink.

Normally, IC Imaging Control shows the live display automatically in its control window. However, using disabling ICImagingControl.LiveDisplay you can display transformed buffers in the control.

The source code for the C# version of this sample program can be under samples\C# *\Display Buffer in your="" My Documents/IC Imaging Control 3.5 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 device, input and video format as shown in the First Steps Visual Studio .NET 2010 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 making a selection, the program displays an error message and terminates.

Disabling the Automatic Live Display

Set LiveDisplay in the property browser or in Form1_Load to False. This prevents IC Imaging Control from displaying the live stream automatically.

Adding Initialization Code

Create a Form_Load event procedure and add the following code:

[C#]
private void Form1_Load( object sender, EventArgs e ) { if( !icImagingControl1.LoadShowSaveDeviceState("lastSelectedDeviceState.xml") ) { MessageBox.Show("No device was selected.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); return; } // Change display dimensions to stretch video to full control size icImagingControl1.LiveDisplayDefault = false; icImagingControl1.LiveDisplaySize = icImagingControl1.Size; cmdStop.Enabled = false; icImagingControl1.LiveDisplay = false; InitSink(); }

This opens the last used device or allowas users to choose a new device. If no device is selected, quits.

The line with ControlName.LiveDisplaySize = ControlName.Size stretches the displayed images to the control dimensions.

ControlName.LiveDisplay = false disables the live video so that DisplayImageBuffer can show its buffers in the control space.

Calling InitSink initializes the sink with the actual callback function.

The 'New Image' event

Now, add an InitSink method to initialize the sink:

[C#]
private void InitSink() { icImagingControl1.Sink = new FrameQueueSink(ShowBuffer, MediaSubtypes.RGB32, 5); }

This creates a FrameQueueSink with will call the ShowBuffer for each new frame. This sink will have a queue size of 5 and only accepts MediaSubtypes.RGB32 image buffers.

Now, add a ShowBuffer method which will be called for each new frame.

[C#]
private FrameQueuedResult ShowBuffer( IFrameQueueBuffer buffer ) { try { icImagingControl1.DisplayImageBuffer( buffer ); } catch( Exception ex ) { System.Diagnostics.Trace.WriteLine( ex.Message ); } return FrameQueuedResult.ReQueue; }

This method will be called for each new frame. It calls ICImagingControl.DisplayImageBuffer which takes the frame and copies its contents into a paint back buffer. Then FrameQueuedResult.ReQueue to indicate we won't hang on to the passed in IFrameQueueBuffer and the sink itself should re-queue the buffer on its own.

Implementing Start/Stop Commands

Add a button to the form, set its Caption to "Start" and its Name to "cmdStart". Add the following code to its click event procedure:

[C#]
private void cmdStart_Click( object sender, EventArgs e ) { icImagingControl1.LiveStart(); cmdStart.Enabled = false; cmdStop.Enabled = true; }

Add a second button to the form, set its Caption to "Stop" and its Name to "cmdStop". Add the following code to its click event procedure:

[C#]
private void cmdStop_Click( object sender, EventArgs e ) { cmdStart.Enabled = true; cmdStop.Enabled = false; icImagingControl1.LiveStop(); icImagingControl1.DisplayImageBufferClear(); }

<< Programmer's Guide