// Event Handler for DepthFrameReady events
 // (A new frame of DepthStream data is available)
 private void KinectDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
 {
     // Get the current DepthImageFrame
     using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
     {
         if (depthFrame != null)
         {
             try
             {
                 // Get the depth data from the DepthImageFrame
                 depthFrame.CopyDepthImagePixelDataTo(depthStreamData);
                 int minDepth = depthFrame.MinDepth;
                 int maxDepth = depthFrame.MaxDepth;
                 // Loop through each pixel and convert to RGB
                 int colorPixelIndex = 0;
                 for (int i = 0; i < depthStreamData.Length; ++i)
                 {
                     short depth = depthStreamData[i].Depth;
                     byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
                     depthRgbData[colorPixelIndex++] = intensity;
                     depthRgbData[colorPixelIndex++] = intensity;
                     depthRgbData[colorPixelIndex++] = intensity;
                     colorPixelIndex++;
                 }
                 // Write the converted depth data to the depthBitmap image
                 depthBitmap.WritePixels(new Int32Rect(0, 0, depthBitmap.PixelWidth, depthBitmap.PixelHeight), depthRgbData, depthBitmap.PixelWidth * sizeof(int), 0);
                 // Dispatch the DepthDataChange event
                 DepthDataChangeEventArgs d = new DepthDataChangeEventArgs(depthStreamData, depthRgbData);
                 DepthDataChange(d);
             }
             catch (NullReferenceException ex)
             {
                 Console.WriteLine(ex.TargetSite + " - " + ex.Message);
             }
         }
     }
 }
 protected virtual void DepthDataChange(DepthDataChangeEventArgs e)
 {
     if (DepthDataChanged != null)
     {
         DepthDataChanged(this, e);
     }
 }