KinectTableExampleCSharp.ImageFrameConverter.SetDepthImage C# (CSharp) Method

SetDepthImage() public static method

Convert the depth image frame to a color bitmap.
public static SetDepthImage ( Bitmap bitmap, ImageFrame image ) : void
bitmap System.Drawing.Bitmap
image ImageFrame
return void
        public static void SetDepthImage(Bitmap bitmap, ImageFrame image)
        {
            unsafe
            {
                Debug.Assert(image.bytesPerPixel == 2);

                Rectangle rectangle = new Rectangle(0, 0, image.width, image.height);
                BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.WriteOnly, bitmap.PixelFormat);

                const int pixelSize = 3;

                for (int y = 0; y < image.height; y++)
                {

                    //get the data from the new image
                    byte* nRow = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);

                    for (int x = 0; x < image.width; x++)
                    {
                        int byteNum = (y * image.width + x) * image.bytesPerPixel;
                        byte value = (byte)(((image.Bytes[byteNum] << 8) + image.Bytes[byteNum + 1]) / 1000); //8191);

                        //set the new image's pixel to the grayscale version
                        nRow[x * pixelSize] = value; //B
                        nRow[x * pixelSize + 1] = value; //G
                        nRow[x * pixelSize + 2] = value; //R
                    }
                }

                //unlock the bitmaps
                bitmap.UnlockBits(bitmapData);

                return;
            }
        }

Usage Example

Example #1
0
        /// <summary>
        /// Is fired whenever KinectTable has new data ready.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        void client_DataReady(object sender, DataReadyEventArgs args)
        {
            // Get the current data
            args.GetData(out data);

            // Show color image if available
            if (data.Available.colorImageEnable)
            {
                ImageFrameConverter.SetColorImage(colorBitmap, data.ColorImage);
                pictureBoxColor.Image = colorBitmap;
            }

            // Show depth image if available
            if (data.Available.depthImageEnable)
            {
                ImageFrameConverter.SetDepthImage(depthBitmap, data.DepthImage);
                pictureBoxDepth.Image = depthBitmap;
            }

            // Show test image if available
            if (data.Available.testImageEnable)
            {
                ImageFrameConverter.SetColorImage(testBitmap, data.TestImage);
                pictureBoxTest.Image = testBitmap;
            }

            // Create demo image and show it
            bool createdDemoImage = DemoImageCreator.CreateDemoImage(demoBitmap, data);

            if (createdDemoImage)
            {
                pictureBoxDemo.Image = demoBitmap;
            }

            return;
        }