Grabbing an Image

This article shows in detail how to set up a data stream from a video capture device and grab a single image.

Opening and Configuring the Video Capture Device

First, the library is initialized, and the first available video capture device is opened:

// Initialize the library
ic4.Library.Init();

// Create a grabber object
var grabber = new ic4.Grabber();

// Open the first available video capture device
var firstDevInfo = ic4.DeviceEnum.Devices.First();
grabber.DeviceOpen(firstDevInfo);

Console.WriteLine($"Opened device {grabber.DeviceInfo.ModelName}");

Then, the device has to be configured. This step is important because in most situations, programs want the camera to be in a defined state before starting operation. In this example, the resolution is configured using the device's Width and Height properties:

// Set the resolution to 640x480
grabber.DevicePropertyMap.SetValue(ic4.PropId.Width, 640);
grabber.DevicePropertyMap.SetValue(ic4.PropId.Height, 480);

At this point, an application could also load a prepared device configuration file (using Grabber.DeviceOpenFromState) or apply a serialized property configuration using PropertyMap.DeSerialize.

Setting up the Sink and Data Stream

After the device has been configured, it is time to setup a data stream. To receive image data from the video capture device, a Sink object has to be created. The SnapSink is the sink most suitable to grab images on demand.

// Create a SnapSink. A SnapSink allows grabbing single images (or image sequences) out of a data stream.
var sink = new ic4.SnapSink();
// Setup data stream from the video capture device to the sink and start image acquisition.
grabber.StreamSetup(sink, ic4.StreamSetupOption.AcquisitionStart);

The data stream is established by calling Grabber.StreamSetup, passing the sink as a parameter. We also set the setupOption parameter to StreamSetupOption.AcquisitionStart, so that the device is instructed to start image acquisition immediately after the data stream was created.

After the StreamSetup call returned successfully, the device is continuously sending images to the host computer.

Grabbing an Image

By calling SnapSink.SnapSingle, the sink is instructed to wait for the next image to arrive at the sink and, if an image is received during the specified timeout period, return it:

try
{
    // Grab a single image out of the data stream.
    var image = sink.SnapSingle(TimeSpan.FromSeconds(1));

    // Print image information.
    Console.WriteLine($"Received an image. ImageType: {image.ImageType}");

    // Save the image.
    // Extension method syntax required using ic4; at the top of this file.
    image.SaveAsBitmap("test.bmp");
}
catch(ic4.IC4Exception ex)
{
    Console.WriteLine(ex.Message);
}

In this example, we print information about the received image and save it in a bitmap file.

Both SnapSink.SnapSingle and SaveAsBitmap could potentially fail. Therefore, we wrap the code into a try..catch block to print the error message in case an error occurs.

Stopping the Data Stream

A call to Grabber.StreamStop stops the data stream:

// Stop the data stream.
grabber.StreamStop();

Stopping acquisition and data stream is important, because keeping the acquisition active would waste CPU and memory resources as well as bandwidth on the transmission medium.