Getting started with Microsoft Kinect SDK
Written by Mike James   
Friday, 17 June 2011
Article Index
Getting started with Microsoft Kinect SDK
Hello Kinect in Windows Forms
Working in WPF

 

If you want to do the same job in WPF then simply change function to:

BitmapSource PImageToBitmapSource(
 PlanarImage PImage)
{
BitmapSource bmap = BitmapSource.Create(
PImage.Width,
PImage.Height,
96, 96,
PixelFormats.Bgr32,
null,
PImage.Bits,
PImage.Width * PImage.BytesPerPixel);
return bmap;
}

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.

Now with this function we can complete the event handler with the single line:

Bitmap bmap = PImageToBitmap(Image);

The Bitmap that results can now be used in a PictureBox to display the image. So place a PictureBox control on the form and add the final line:

 pictureBox1.Image = bmap;
}

To make the WPF version work place an Image control on the form and set its source to the BitmapSource returned by the WPF version of the function.

Finally we need to clean up by calling the Uniitialize method when the program closes or when we have finished using the Kinect:

private void Form1_FormClosing(
object sender, FormClosingEventArgs e)
{
nui.Uninitialize();
}

If you now run the program - with the Kinect plugged in of course - you will see a video displayed in the PictureBox.

From 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(
object sender, EventArgs e)
{
nui.NuiCamera.ElevationAngle += 4;
}
private void button2_Click(
object sender, EventArgs e)
{
nui.NuiCamera.ElevationAngle -= 4;
}

 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.

The complete listing of the finished program is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Drawing.Imaging;
using System.Runtime.InteropServices;

using Microsoft.Research.Kinect.Nui;
using Microsoft.Research.Kinect.Audio;

namespace HelloKinectWorld
{
public partial class Form1 : Form
{
 Runtime nui = Runtime.Kinects[0];

public Form1()
{
InitializeComponent();
}

private void Form1_Load(
object sender, EventArgs e)
{
nui.Initialize(RuntimeOptions.UseColor);
nui.VideoStream.Open(
ImageStreamType.Video,
2,
ImageResolution.Resolution640x480,
 ImageType.Color);
nui.VideoFrameReady +=
new EventHandler<ImageFrameReadyEventArgs>(
FrameReady);
}

void FrameReady(object sender,
ImageFrameReadyEventArgs e)
{
PlanarImage Image = e.ImageFrame.Image;
Bitmap bmap = PImageToBitmap(Image);
pictureBox1.Image = bmap;
}

Bitmap PImageToBitmap(PlanarImage PImage)
{
Bitmap bmap = new Bitmap(
PImage.Width,
PImage.Height,
PixelFormat.Format32bppRgb);
BitmapData bmapdata = bmap.LockBits(
new Rectangle(0, 0, PImage.Width,
PImage.Height),
 ImageLockMode.WriteOnly,
bmap.PixelFormat);
IntPtr ptr = bmapdata.Scan0;
Marshal.Copy(PImage.Bits,
0,
ptr,
PImage.Width *
PImage.BytesPerPixel *
 PImage.Height);
bmap.UnlockBits(bmapdata);
return bmap;
}

private void button1_Click(
object sender, EventArgs e)
{
nui.NuiCamera.ElevationAngle += 4;
}

private void button2_Click(
object sender,
EventArgs e)
{
nui.NuiCamera.ElevationAngle -= 4;
}
  private void Form1_FormClosing(
object sender,
FormClosingEventArgs e)
{
nui.Uninitialize();
}
 }
}

More articles:

 

Going Deeper - literally because we explore the depth field

The Player Index - working with processed data

You can download the code for both the Windows Forms version and the WPF version in a single ZIP file from the CodeBin (note you have to register first).

Other Articles in this Series

  1. Getting started with Microsoft Kinect SDK (This article)
  2. Depth
  3. Player index
  4. Depth and Video space
  5. Skeletons
  6. The Full Skeleton

 

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter or Facebook or you can subscribe to our weekly newsletter.

 

Further reading:

On Kinect:

All About Kinect

Getting Started with PC Kinect using the open source drivers

Kinect goes self aware - a warning well we think it's funny and something every Kinect programmer should view before proceding to create something they regret!

On WPF Bitmap handling:

BitmapSource: WPF Bitmaps

BitmapImage

WriteableBitmap

Custom BitmapSource

 

<ASIN:0672333457>

<ASIN:1449394620>



Last Updated ( Monday, 06 February 2012 )