private Bitmap ConvertDepthToBitmap(PXCMImage.ImageData depthData, PXCMCapture.Sample sample)
        {
            Bitmap bmp = new Bitmap(sample.depth.info.width, sample.depth.info.height);

            var size = sample.depth.info.width * sample.depth.info.height;

            Int16[] values      = new Int16[size];
            var     depthValues = depthData.ToShortArray(0, values);

            var   minDistance = 500;  // mm
            var   maxDistance = 2000; // mm
            float scale       = 255.0f / (maxDistance - minDistance);

            var dataPoint = new DataPoint
            {
                nearest = new Coordinate {
                    x = -1, y = -1, z = short.MaxValue
                },
                farthest = new Coordinate {
                    x = -1, y = -1, z = short.MinValue
                }
            };
            var i      = 0;
            var sum    = 0.0;
            var width  = sample.depth.info.width;
            var height = sample.depth.info.height;

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    var distance   = depthValues[i++]; // mm
                    var brightness = 0;

                    if (distance > 0 && distance < dataPoint.nearest.z)
                    {
                        dataPoint.nearest = new Coordinate {
                            x = (double)x / width, y = (double)y / height, z = distance
                        }
                    }
                    ;
                    if (distance > dataPoint.farthest.z)
                    {
                        dataPoint.farthest = new Coordinate {
                            x = (double)x / width, y = (double)y / height, z = distance
                        }
                    }
                    ;
                    sum += distance;

                    if (distance > minDistance && distance < maxDistance)
                    {
                        brightness = 255 - (int)((distance - minDistance) * scale);
                    }
                    System.Drawing.Color color = System.Drawing.Color.FromArgb(brightness, brightness, brightness);
                    bmp.SetPixel(x, y, color);
                }
            }

            if (dataPoint.nearest.z == short.MaxValue)
            {
                dataPoint.nearest = null;
            }
            if (dataPoint.farthest.z == short.MinValue)
            {
                dataPoint.farthest = null;
            }
            dataPoint.averageDepth = sum / size;
            dataPoint.outOfBed     = OutOfBed;
            dataPoint.timeStamp    = DateTime.UtcNow;
            SendDataPoint(dataPoint);

            return(bmp);
        }