using Microsoft.Kinect; using System; using System.Collections.Generic; using System.Diagnostics; 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 WpfApp { /// /// Logique d'interaction pour MainWindow.xaml /// public partial class MainWindow : Window { private KinectSensor kinectSensor = null; private ColorFrameReader colorFrameReader = null; private WriteableBitmap colorBitmap = null; public MainWindow() { InitializeComponent(); Debug.WriteLine("Initialisation de Kinect..."); // Initialiser la Kinect this.kinectSensor = KinectSensor.GetDefault(); if (this.kinectSensor != null) { Debug.WriteLine("Kinect détecté."); Debug.WriteLine("Ouverture du capteur Kinect."); // Ouvrir le lecteur de flux de couleur this.colorFrameReader = this.kinectSensor.ColorFrameSource.OpenReader(); // Frame description pour les images de couleur FrameDescription colorFrameDescription = this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra); // Créer le bitmap pour afficher l'image this.colorBitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null); // Gérer l'événement FrameArrived pour le flux de couleur this.colorFrameReader.FrameArrived += this.Reader_ColorFrameArrived; // Ouvrir la Kinect this.kinectSensor.Open(); } else { Debug.WriteLine("Aucune Kinect détectée."); } } private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { Debug.WriteLine("Frame de couleur arrivée."); using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { Debug.WriteLine("Traitement de la frame de couleur."); FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); // Vérifier si la taille de l'image a changé if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } Debug.WriteLine("Frame de couleur traitée."); } else { Debug.WriteLine("Frame de couleur nulle."); } } } // Assurez-vous de fermer correctement le lecteur et le capteur Kinect lors de la fermeture de la fenêtre private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { Debug.WriteLine("Fermeture de l'application et libération des ressources Kinect."); if (this.colorFrameReader != null) { this.colorFrameReader.Dispose(); this.colorFrameReader = null; } if (this.kinectSensor != null) { this.kinectSensor.Close(); this.kinectSensor = null; } Debug.WriteLine("Ressources Kinect libérées."); } } }