public partial class MainWindow : Window
private BitmapSource CreateImageFromDepthImage(ImageFrame image)
int width = image.Image.Width;
int height = image.Image.Height;
var depthFrame = image.Image.Bits;
// We multiply the product of width and height by 4 because each byte
// will represent a different color channel per pixel in the final iamge.
var colorFrame = new byte[height * width * 4];
// We use the previously built depthIndexes IEnumerable collection for this purpose
// For each pixel in the final image, there are two bytes that represent the depth in
// the Kinect depth image. To utilize the Parallel ForEach loop efficiently, we use a
// prebuilt collection of primary indexes for these positions.
Parallel.ForEach(depthIndexes, depthIndex =>
// Because the colorFrame we are creating has twice as many bytes representing
// a pixel in the final image, we set the index to be twice of the depth index.
var index = depthIndex * 2;
// Calculate the distance represented by the two depth bytes
var distance = CalculateDistanceFromDepth(depthFrame[depthIndex], depthFrame[depthIndex + 1]);
// Map the distance to an intesity that can be represented in RGB
var intensity = CalculateIntensityFromDistance(distance);
// Apply the intensity to the color channels
colorFrame[index + BlueIndex] = intensity;
colorFrame[index + GreenIndex] = intensity;
colorFrame[index + RedIndex] = intensity;
return BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgr32, null, colorFrame, width * PixelFormats.Bgr32.BitsPerPixel / 8);
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Open Systems Technologies
First learned to program in 1997 on my TI-83 and have been doing it ever since, with a foray into networking and infrastructure.
Mostly a C# junky (Win\Web Forms, WP7.5/8, WPF and MVC), though I have experience with many other technologies and products.
I have also been trying to learn and apply more in the area of AI; focusing on computer vision, natural language processing, and classification.
In my spare time, I love to tinker with electronics and various useless DIY projects.
My brain is a shark... if it stops moving, it will die. I'm always looking to learn more.