This article illustrates a basic introductory level explanation of the steps required to interface with the Microsoft Kinect for Windows sensor using the Kinect for Windows Sdk implemented in a Windows Forms application.
Most of the introduction level articles and code samples I’ve come across tend to be implemented as WPF or XNA based applications. Interfacing to the Microsoft Kinect for Windows sensor from Windows Forms based applications can be achieved fairly effortlessly. I find when wanting to create test applications a Windows Forms implementation can be developed quickly with relatively little hassle.
Sample source code
Sample Source Overview
The sample source code provided with this article consists of a Windows Form which host a PictureBox control used to display the incoming Video feed. It is possible that more than one Kinect sensor might be connected, the user can therefore select a sensor from a ComboBox. Possible resolutions/frame rates can also be configured via an additional ComboBox.
Available Kinect Sensors
The sample application implements the KinectSensor.KinectSensors property to retrieve a list of available Kinect sensor. The associated ComboBox will be populated with references to available sensors.
Initial setup: Form Constructor
In the Form constructor we define the available display modes by adding the relevant enumeration values to the associated ComboBox.
In this scenario we specify:
In the constructor the method PopulateAvailableSensors(), as described earlier, will be invoked after having specified available display modes.
Refreshing Available Sensors
Clicking the refresh button results in calling PopulateAvailableSensors(), which will re-determine available sensors and populate the associated ComboBox. Listed below is the click event handler subscribed to the Refresh button’s click event.
Activating the Sensor
When clicking on the “Activate Sensor” button the sample code first attempts to deactivate the sensor currently in use. After ensuring that resources related to the current sensor have been released the application attempts to assign an instance of the KinectSensor member object based on user selection.
Sensor Video setup
In the sample application we implement the Kinect sensor’s ColorStream camera. The Kinect sensor exposes an event which informs subscribed event handlers when a video frame is available for processing. The following code snippet shows the code implemented in setting up the video format.
Processing Video Frames
The ColorFrameReady event informs subscribed event handlers whenever a Color frame becomes available for processing.
The event handler parameters provides access to a ColorImageFrame object. Before the video frame can be displayed the ColorImageFrame object first needs to be converted to a memory Bitmap, which can then be displayed by the Form’s PictureBox. Notice the ColorImageFrameToBitmap method, it functions by extracting image data from the ColorImageFrame as a byte array, then creates a new memory Bitmap and copies the byte array of image data.
Deactivating a sensor
Earlier when we activated the sensor we deactivated the active sensor. Listed below is the relevant code snippet.