Kinect SDK 1 - Skeletons
Written by Mike James   
Article Index
Kinect SDK 1 - Skeletons
Using the GDI
Joints
From 3D to 2D

 

Using the GDI

The key idea in the way that Windows Forms provides the facilities of the GDI is the Graphics object. This is a wrapper around the underlying operating system graphics used to draw everything you see on the screen.To make use of it you need to create a Graphics object which provides all of the methods you need to draw on that object. Clearly not all objects can be drawn on and not all objects can be associated with a Graphics object  - but a Bitmap can.

So if you have converted the bit array into a Bitmap

Bitmap bmap=ImageToBitmap(videoimage);

using the function introduced in chapter 2, you can obtain a Graphic object to enable you to draw on the Bitmap.

For completeness here is the listing of the ImageToBitmap function - for an explaination refer to chapter 2:

Bitmap ImageToBitmap(
ColorImageFrame Image)
{
byte[] pixeldata =
new byte[Image.PixelDataLength];
Image.CopyPixelDataTo(pixeldata);
Bitmap bmap = new Bitmap(
Image.Width,
Image.Height,
PixelFormat.Format32bppRgb);
BitmapData bmapdata = bmap.LockBits(
new Rectangle(0, 0,
Image.Width, Image.Height),
  ImageLockMode.WriteOnly,
bmap.PixelFormat);
IntPtr ptr = bmapdata.Scan0;
Marshal.Copy(pixeldata, 0, ptr,
 Image.PixelDataLength);
bmap.UnlockBits(bmapdata);
return bmap;
}

Once you have a Bitmap of the video data then you can process it using the standard facilities provided by the API. In particular you can obtain a Graphics object:

Graphics g = Graphics.FromImage(bmap);

Now you can use the drawing methods provided by Graphics to draw on the Bitmap. So for example:

g.DrawEllipse(Pens.Red, 
new Rectangle(x - 10, y - 10, 20, 20));

will draw an ellipse on the bitmap as specified by the Pen and the Rectangle which contains the ellipse.

Now that you have seen how this works you can look up the drawing methods of the Graphics object and use them to draw almost anything on the Bitmap. For eaxmple to draw a line you can use:

DrawLine(Pen, x1,y1,x2,y2);

which draws a line in the specified Pen from x1,y1 to x2,y2 and

DrawLine(Pen,Point1,Point2);

which draws a line from Point1 to Point2.

Notice that all of the co-ordinates are in pixels and so as long as we convert Kinect co-ordinates to pixel co-ordinates in the video Bitmap we can just use them to draw.

 

Displaying the Video

With this all of this explained we can convert the raw data into a bitmap and display it. Place a PictureBox on the form and add:

 Bitmap bmap = ImageToBitmap(VFrame);
pictureBox1.Image = bmap;
}

To the end of the FrameReady method. You also need to add:

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

The complete FrameReady method is:

void FramesReady(object sender, 
AllFramesReadyEventArgs e)
{
ColorImageFrame VFrame =
e.OpenColorImageFrame();
if (VFrame == null) return;
Bitmap bmap = ImageToBitmap(VFrame);
pictureBox1.Image = bmap;
}

If you run the program as it is you will see the video captured by the Kinect displayed in the PictureBox.

To make sure that the program ends without an error you also need to add the FormClosing event handler:

private void Form1_FormClosing(
object sender,
FormClosingEventArgs e)
{
sensor.Stop();
}