1. Create depth image and control mouse

This example takes the depth sensor info and creates an image. There is also one line of code that controls the mouse so that it clicks in the top left corner. Importantly, this click is outside the bounds of the window. This means this code has the potential to control other software through mouse controls.

Download the Visual Studio Project for this code

The above project includes the necessary DLL files so that you can immediately run the project.

Here’s the code included in the project above:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using Microsoft.Kinect;
using KinectMouseController;

namespace WpfApp1
{

    public partial class MainWindow : Window
    {

        KinectSensor sensor;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void loaded(object sender, RoutedEventArgs e)
        {
            if(KinectSensor.KinectSensors.Count > 0)
            {
                sensor = KinectSensor.KinectSensors[0];
                
                if(sensor.Status == KinectStatus.Connected)
                {
                    sensor.ColorStream.Enable();
                    sensor.DepthStream.Enable();
                    sensor.SkeletonStream.Enable();
                    sensor.AllFramesReady += Sensor_AllFramesReady;
                    sensor.Start();
                }
            }

            int cursorX = 0;
            int cursorY = 0;

            KinectMouseController.KinectMouseMethods.SendMouseInput(cursorX, cursorY,(int) System.Windows.SystemParameters.PrimaryScreenWidth, (int) System.Windows.SystemParameters.PrimaryScreenHeight, true);
        }

        private void Sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if(depthFrame == null)
                {
                    return;
                }

                byte[] pixels = GenerateColouredBytes(depthFrame); 
                

                int stride = depthFrame.Width * 4;
                image1.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
            }
        }

        private byte[] GenerateColouredBytes(DepthImageFrame depthFrame)
        {
            DepthImagePixel[] rawDepthData = new DepthImagePixel[depthFrame.PixelDataLength];
            depthFrame.CopyDepthImagePixelDataTo(rawDepthData);

            Byte[] colourPixels = new byte[depthFrame.Height * depthFrame.Width * 4];

            const int BlueIndex = 0;
            const int GreenIndex = 1;
            const int RedIndex = 2;

            for(int depthIndex = 0, colorIndex = 0;
                depthIndex < rawDepthData.Length && colorIndex < colourPixels.Length;
                depthIndex++, colorIndex+=4)
            {
                int depth = rawDepthData[depthIndex].Depth; 
                colourPixels[colorIndex + BlueIndex] = (byte) depth;
                colourPixels[colorIndex + GreenIndex] = (byte) depth;
                colourPixels[colorIndex + RedIndex] = (byte) depth;
            }

            return colourPixels;

        }

        private void closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            sensor.Stop();
            sensor.AudioSource.Stop();
        }
    }
}