Skip to content

Instantly share code, notes, and snippets.

@kamiyaowl
Created March 2, 2014 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamiyaowl/9309430 to your computer and use it in GitHub Desktop.
Save kamiyaowl/9309430 to your computer and use it in GitHub Desktop.
kinect for c#動作最小コード
<Window x:Class="KinectTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Initialized="Window_Initialized" Closed="Window_Closed">
<Grid>
<Image x:Name="XImageRGB" HorizontalAlignment="Left" Height="299" Margin="10,10,0,0" VerticalAlignment="Top" Width="497"/>
</Grid>
</Window>
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace KinectTest
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Initialized(object sender, EventArgs e)
{
if (KinectSensor.KinectSensors.Count == 0)
{
throw new Exception("Kinect not detected.");
}
//Initialize Kinect
var kinect = KinectSensor.KinectSensors[0];
kinect.ColorStream.Enable();
kinect.ColorFrameReady += kinect_ColorFrameReady;
kinect.Start();
}
void kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
{
using (var cf = e.OpenColorImageFrame())
{
if (cf != null)
{
var cp = new byte[cf.PixelDataLength];
cf.CopyPixelDataTo(cp);
XImageRGB.Source = BitmapSource.Create(cf.Width, cf.Height, 96, 96, PixelFormats.Bgr32, null, cp, cf.Width * cf.BytesPerPixel);
}
}
}
private void Window_Closed(object sender, EventArgs e)
{
foreach (var k in KinectSensor.KinectSensors)
{
if (k == null) continue;
k.ColorFrameReady -= kinect_ColorFrameReady;
k.Stop();
k.Dispose();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment