The goal of the code below is to connect to Kinect device, and load the video stream to an image control that is placed on the UI. The name of image control is “imgKinect”.

UI CODE
=======
This is some source code:




    
        

            
            
            
            
            
            
        
    


Code Behind
===========


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit;

using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace KinectDemo1
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        private static readonly int Bgr32BytesPerPixel = (PixelFormats.Bgr32.BitsPerPixel + 7) / 8;
        private readonly KinectSensorChooser sensorChooser = new KinectSensorChooser();
        private WriteableBitmap colorImageWritableBitmap;
        private byte[] colorImageData;
        private ColorImageFormat currentColorImageFormat = ColorImageFormat.Undefined;
        bool closing = false;

        public MainWindow()
        {
            InitializeComponent();

            sensorChooser.KinectChanged += sensorChooser_KinectChanged;
            sensorChooser.Start();
           
        }

        void _sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            if (closing)
            {
                return;
            }      
        }

        void sensorChooser_KinectChanged(object sender, KinectChangedEventArgs e)
        {
            KinectSensor oldSensor = e.OldSensor;
            StopKinect(oldSensor);

            KinectSensor newSensor = e.NewSensor;

            this.colorPixels = new byte[newSensor.ColorStream.FramePixelDataLength];

            // This is the bitmap we'll display on-screen
            this.colorBitmap = new WriteableBitmap(newSensor.ColorStream.FrameWidth, newSensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
            this.colorBitmapDepth = new WriteableBitmap(newSensor.ColorStream.FrameWidth, newSensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
            
            newSensor.AllFramesReady += new EventHandler<allframesreadyeventargs>(_sensor_AllFramesReady);
            newSensor.ColorFrameReady += newSensor_ColorFrameReady;

            newSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);

            try
            {
                newSensor.Start();
            }
            catch (System.IO.IOException)
            {
                sensorChooser.TryResolveConflict();
            }
        }
                
        private WriteableBitmap colorBitmap;

        private byte[] colorPixels;

        void newSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {

            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    // Copy the pixel data from the image to a temporary array
                    colorFrame.CopyPixelDataTo(this.colorPixels);

                    // Write the pixel data into our bitmap
                    this.colorBitmap.WritePixels(
                        new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                        this.colorPixels,
                        this.colorBitmap.PixelWidth * sizeof(int),
                        0);
                }
            }
            imgKinect.Source = colorBitmap;

        }

        void StopKinect(KinectSensor sensor)
        {
            if (sensor != null)
            {
                sensor.Stop();
                sensor.AudioSource.Stop();
            }
        }       

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            StopKinect(sensorChooser.Kinect);
        }

    }
}