KinectTableExampleCSharp.ImageFrameConverter.SetBinaryImage C# (CSharp) Method

SetBinaryImage() public static method

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

                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] == 0 ? 0 : 255);

                        //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;
            }
        }