Search code examples
c#wpfxamlkinectkinect-sdk

How to pass skeletal data in Kinect Interaction Sample using ViewModel


I am playing a bit with newest SDK (1.7) and would like to make some changes in one of the examples.

I have a project called KinectAttractWindow where I would like to have data from skeletal, depth or color image but I am not able to figure out how to pass these data between view and viewmodel. For example in this HomeScreenViewModel I would like to draw a skeleton using this HomeScreenView. Or how to display depth or color data using the same project architecture?

How to do it in a proper way? Do you hany any advice for me?

I have update my HomeView and ViewModel but I get a NullReferenceException here:

'this.RGBImage.DisplayImage.Source =
                BitmapSource.Create(colorFrame.Width,
                colorFrame.Height,
                96,
                96,
                PixelFormats.Bgr32,
                null,
                pixels,
                stride);'

Solution

  • First of allmake sure that your object RGBImage and its property DisplayImage aren't null.I use WriteableBitmap to show my RGB values since it creates one WriteableBitmap-object and rewrites pixels to it so that performance is better. You can find more information on WriteableBitmap here.

    You can use it like this -

    WriteableBitmap wBitmap = new WriteableBitmap(colorFrame.Width,
                                                      colorFrame.Height,
                                                      // Standard DPI
                                                      96, 96,
                                                      // Current format for the ColorImageFormat
                                                      PixelFormats.Bgr32,
                                                      // BitmapPalette
                                                      null);
    

    Write new pixels to the object by doing this -

    wBitmap.WritePixels(
                    // Represents the size of our image
                   new Int32Rect(0, 0, colorFrame.Width, colorFrame.Height),
                    // Our image data
                   _pixelData,
                    // How much bytes are there in a single row?
                   colorFrame.Width * colorFrame.BytesPerPixel,
                    // Offset for the buffer, where does he need to start
                   0);
    

    Assign it to your image control -

    this.RGBImage.DisplayImage.Source  = wBitmap;
    

    Skeleton data

    You can do skeletal tracking exactly the same as color data, you Enable() the stream, process the data coming in at the SkeletonFrameReady and save all the data in properties in your ViewModel. By doing that it gives you the ability to databind to these properties.