Kinect for Windows SDK– color stream

 

Color Stream in WPF Application

Add Image control in MainWindow.xaml in the Window Grid

 <Image Name="kinectColorImage" />
 Setup Kinect sensor object to retrieve first available sensor, other variables to hold color pixel data and a writeable bitmap object to write pixel onto it.
 KinectSensor sensor = KinectSensor.KinectSensors[0];
private byte[] colorPixelData;
private WriteableBitmap outputImage;    

 Add enabling color stream in MainWindow() constructor of Main.Window.xaml, using sensor object, enable color stream, also setting up rgb resolution to 
640x480 at the 30 frames per seconds(FPS). Add color frame ready handler so that color frame stream can be handled appropriately. And invoke Start method 
of sensor to start the sensor, which will actually start receiving streams from the sensor.
 sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
sensor.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(sensor_ColorFrameReady);
sensor.Start();
 Color Image formats on MSDN

 Add color frame ready handler,which will invoke as sensor starts receiving color frame includes arg e, which can be used to open the color image frame, 
CopyPixeDataTo the colorPixelData byte array declared earlier, use WriteableBitmap for desired width and height of the bitmap, horizontal and vertical 
dots per inch of the bitmap, PixelFormat and null bitmap palette.later use WritePixels method to update the pixels in the specified region of the bitmap, 
create a rectangle using 0,0, width and height of the color frame, using colorPixelData for input buffer used for updating bitmap, size of the buffer 
and stride, which is 0, and finally assign outputImage to the source of image control named kinectColorImage.
       void sensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    //Using standard SDK
                    this.colorPixelData = new byte[colorFrame.PixelDataLength];

                    colorFrame.CopyPixelDataTo(this.colorPixelData);

                    this.outputImage = new WriteableBitmap(
                    colorFrame.Width,
                    colorFrame.Height,
                    96,  // DpiX
                    96,  // DpiY
                    PixelFormats.Bgr32,
                    null);

                    this.outputImage.WritePixels(
                    new Int32Rect(0, 0, colorFrame.Width, colorFrame.Height),
                    this.colorPixelData,
                    colorFrame.Width * 4,
                    0);
                    this.kinectColorImage.Source = this.outputImage;

                    //Using Coding4Fun Kinect Toolkit
                    //kinectColorImage.Source = imageFrame.ToBitmapSource();

                }
            }
          }

or, in case you want to use Code4fun Kinect toolkit, install it via NuGet Install-Package Coding4Fun.Kinect.Wpf and use ToBitmapSource() method to do the same stuff. Happy Kinecting!!!