Click here to Skip to main content
15,878,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am new with kinect, I know how to do it in WPF but I have problem for capturing depth in windows application.

I found some code as below:

private void Kinect_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
   using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
      if (depthFrame != null)
         Bitmap DepthBitmap = new Bitmap(depthFrame.Width, depthFrame.Height,     PixelFormat.Format32bppRgb);
         if (_depthPixels.Length != depthFrame.PixelDataLength)
            _depthPixels = new DepthImagePixel[depthFrame.PixelDataLength];
            _mappedDepthLocations = new ColorImagePoint[depthFrame.PixelDataLength];
         //Copy the depth frame data onto the bitmap  
         var _pixelData = new short[depthFrame.PixelDataLength];
         BitmapData bmapdata = DepthBitmap.LockBits(new Rectangle(0, 0, depthFrame.Width,
            depthFrame.Height), ImageLockMode.WriteOnly, DepthBitmap.PixelFormat);
         IntPtr ptr = bmapdata.Scan0;
         Marshal.Copy(_pixelData, 0, ptr, depthFrame.Width * depthFrame.Height);
         pictureBox.Image = DepthBitmap;
      // sensor.CoordinateMapper.MapDepthFrameToColorFrame(DepthImageFormat.Resolution640x480Fps30,_depthPixels,ColorImageFormat.RgbResolution640x480Fps30,_mappedDepthLocations);

but this is not giving me the greyScale depth and it's purple. Any improvement or help?

Thanks in advance :)
Updated 5-Jan-14 0:07am
Yvan Rodrigues 4-Jan-14 12:30pm    
What is the problem? Have you tried the examples in the SDK?
niloufar.M 4-Jan-14 19:58pm    
Dear Yvan, all the existing examples are in WPF, I want it in c# (Windows form)
Yvan Rodrigues 4-Jan-14 12:33pm    
I found the walk-through on Channel9 helpful.
niloufar.M 5-Jan-14 2:23am    
Dear Yvan, where can I find that? you mean this one?
niloufar.M 5-Jan-14 2:31am    
They are all in WPF :(

1 solution

I found the solution:

void Kinect_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
if (depthFrame != null)
this.depthFrame32 = new byte[depthFrame.Width * depthFrame.Height * 4];
//Update the image to the new format
this.depthPixelData = new short[depthFrame.PixelDataLength];
byte[] convertedDepthBits = this.ConvertDepthFrame(this.depthPixelData, ((KinectSensor)sender).DepthStream);
Bitmap bmap = new Bitmap(depthFrame.Width, depthFrame.Height, PixelFormat.Format32bppRgb);
BitmapData bmapdata = bmap.LockBits(new Rectangle(0, 0, depthFrame.Width, depthFrame.Height), ImageLockMode.WriteOnly, bmap.PixelFormat);
IntPtr ptr = bmapdata.Scan0;
Marshal.Copy(convertedDepthBits, 0, ptr, 4 * depthFrame.PixelDataLength);
pictureBox2.Image = bmap;


private byte[] ConvertDepthFrame(short[] depthFrame, DepthImageStream depthStream)
//Run through the depth frame making the correlation between the two arrays
for (int i16 = 0, i32 = 0; i16 < depthFrame.Length && i32 < this.depthFrame32.Length; i16++, i32 += 4)
// Console.WriteLine(i16 + "," + i32);
//We don’t care about player’s information here, so we are just going to rule it out by shifting the value.
int realDepth = depthFrame[i16] >> DepthImageFrame.PlayerIndexBitmaskWidth;
//We are left with 13 bits of depth information that we need to convert into an 8 bit number for each pixel.
//There are hundreds of ways to do this. This is just the simplest one.
//Lets create a byte variable called Distance.
//We will assign this variable a number that will come from the conversion of those 13 bits.
byte Distance = 0;
//XBox Kinects (default) are limited between 800mm and 4096mm.
int MinimumDistance = 800;
int MaximumDistance = 4096;
//XBox Kinects (default) are not reliable closer to 800mm, so let’s take those useless measurements out.
//If the distance on this pixel is bigger than 800mm, we will paint it in its equivalent gray
if (realDepth > MinimumDistance)
//Convert the realDepth into the 0 to 255 range for our actual distance.
//Use only one of the following Distance assignments
//White = Far
//Black = Close
//Distance = (byte)(((realDepth – MinimumDistance) * 255 / (MaximumDistance-MinimumDistance)));
//White = Close
//Black = Far
Distance = (byte)(255 - ((realDepth - MinimumDistance) * 255 / (MaximumDistance - MinimumDistance)));
//Use the distance to paint each layer (R G & of the current pixel.
//Painting R, G and B with the same color will make it go from black to gray
this.depthFrame32[i32 + RedIndex] = (byte)(Distance);
this.depthFrame32[i32 + GreenIndex] = (byte)(Distance);
this.depthFrame32[i32 + BlueIndex] = (byte)(Distance);
//If we are closer than 800mm, the just paint it red so we know this pixel is not giving a good value
this.depthFrame32[i32 + RedIndex] = 0;
this.depthFrame32[i32 + GreenIndex] = 0;
this.depthFrame32[i32 + BlueIndex] = 0;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900