Grabbing an Image

This chapter shows you how to grab a still image from the video stream and save it to a .BMP file.

The source code for this sample program can be found under samples\C# *\Grabbing an Image 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 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 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 "Save Bitmap". Name the buttons cmdStartLive, cmdStopLive and cmdSaveBitmap respectively.

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

[C#]
private void cmdStartLive_Click(object sender, EventArgs e) { icImagingControl1.Sink = new TIS.Imaging.FrameSnapSink(); icImagingControl1.LiveStart(); cmdStartLive.Enabled = false; cmdStopLive.Enabled = true; cmdSaveBitmap.Enabled = true; }
[C#]
private void cmdStopLive_Click(object sender, EventArgs e) { icImagingControl1.LiveStop(); cmdStartLive.Enabled = true; cmdStopLive.Enabled = false; cmdSaveBitmap.Enabled = false; }

Note that we added icImagingControl1.Sink = new TIS.Imaging.FrameSnapSink(); in cmdStartLive_Click. This creates a FrameSnapSink and add it as the frame capture sink of the control.

Now we can add the handler for clicking on cmdSaveBitmap:

[C#]
private void cmdSaveBitmap_Click( object sender, EventArgs e ) { TIS.Imaging.FrameSnapSink snapSink = icImagingControl1.Sink as TIS.Imaging.FrameSnapSink; TIS.Imaging.IFrameQueueBuffer frm = snapSink.SnapSingle(TimeSpan.FromSeconds(5)); SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "bmp files (*.bmp)|*.bmp|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; if( saveFileDialog1.ShowDialog() == DialogResult.OK ) { frm.SaveAsBitmap(saveFileDialog1.FileName); } }

The handler does 3 things.

The line TIS.Imaging.FrameSnapSink snapSink = icImagingControl1.Sink as TIS.Imaging.FrameSnapSink; fetches the FrameSnapSink which we registered in cmdStartLive_Click.

The line TIS.Imaging.IFrameQueueBuffer frm = snapSink.SnapSingle(TimeSpan.FromSeconds(5)); fetches the next frame arriving in the sink while at most waiting for 5 seconds.

The remaining lines show a SaveFileDialog and saving the image as a bitmap via FrameExtensions.SaveAsBitmap.

<< Programmer's Guide