Getting started with Windows Kinect SDK 1.0 |
Written by Mike James | |||||
Page 4 of 4
ColorImageFrame - WPFThe only difference between the Windows Forms version and a WPF version is the way the pixel data is converted to a bitmap that can be displayed. If you want to do the same job in WPF then simply change the ImageToBitmap function to: BitmapSource ImageToBitmap( I'm not going to explain this in detail because it is a simple application of the BitmapSource class. See the references at the end of the article for more information on how WPF works with bitmap data. The event handler needs only minor changes: void FrameReady(object sender, To make the WPF version work, place an Image control on the form If you now run the program - with the Kinect plugged in of course - you will see a video displayed in the Image control. Finishing TouchesThe Windows Forms and WPF programs described above are the very simplest examples possible. They lack code to handle the proper selection of the Kinect device to use and they lack any error handling - particularly what to do if a Kinect isn't ready or available. We can deal with such details later. However there are two things we really need to deal with at this early stage. The first is switching off the data streams when the program has finished using them.
private void Form1_FormClosing( The second problem is that, if you stop the application by closing its window, you will see an error reporting that the ColorImageFrame doesn't exist. The reason is that the OpenColorImageFrame method returns null if the image data has been abandoned for one reason or another. The solution is to add a test for a null ColorImageFrame:
void FrameReady(object sender, Another problem with the example is that the byte array, bitmaps and other objects are created each time the event handler runs. This is OK for a demonstration but for a real application you need to write code that creates everything outside of the render loop i.e. the event handler, to minimize the work it has to do. Camera elevationFrom here you can start to investigate the additional features of the video sensor, add a video capture facility for example. You can control the video camera's angle by setting the tilt of the entire Kinect. Add two buttons, label one Up and the other Down and enter:
private void button1_Click( Also take care when using the motor driven elevation control - you can burn the motor out if you make it move to often. The code for the buttons given above doesn't check to make sure that you haven't attempted to move it beyond its allowed range - so you can crash the program. Notice that in general no error handling is included in any of the code so that you can see how it works more easily and in practice you would need to add Try-Catch statements to make sure that the application didn't crash.
You can download the code for both the Windows Forms version and the WPF version in a single ZIP file from the CodeBin. Practical Windows Kinect in C#
|