Away from all the discussions about whether Silverlight is dead or not, The Silverlight 5 RC contains the previously announced P-Invoke feature which enables you to call Win32 style APIs from a trusted Silverlight application.
There are some attempts to use Kinect from Silverlight (here) but this was before MS released the official SDK. we will use Silverlight 5 P-Invoke feature to call the Kinect SDK APIs.
P-Invoke in Silverlight works just like P-Invoke on the desktop. you use the DllImport attribute to import the APIs and you declare in your code the dependent types (Enums, structs, etc.)
I tried to keep the classes and methods in library identical to the ones in the official SDK. I implemented only a couple of the available APIs (with the help of the Coding4Fun Kinect Toolkit and Reflector).
To test the library we will create a simple application:
- Create a new Silverlight 5 application
- Add a reference to the KinectSilverlightLibrary.
- Change the properties of the project: enable running out of browser.
- Change the Out-of-Browser settings to require elevated trust
- In the App.xaml.cs file, define a new property of type Runtime, this property represents the Kinect runtime, in the Application_Startup event handler initialize the runtime and in the Application_Exit event handler shut down the runtime
- In the MainPage.xaml file add two image controls that we will use to display the video and depth streams coming from Kinect
- In the MainPage.xaml.cs add the following lines of code
When the page is loaded we open the video and depth streams and subscribe to the VideoFrameReady and DepthFrameReady events, in the event handler we retrieve the ImageFrame and convert it to a bitmap (I used the WritableBitmapEx library)
Here’s the application running
You are free to continue adding the rest of the APIs to this library.
You can download the source code from here: