Aharris

Aharris Aharris

Niner since 2012

Comments

  • Camera Fundamentals

    I'm pretty new at this and have been following the videos. While extremely helpful I've come to an issue I can't resolve. No matter what I try the kinect would display the RGB Image

    I've followed both parts 2 and 3 but for some reason it still wont display my image.

     

    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                kinectSensorChooser1.KinectSensorChanged += new DependencyPropertyChangedEventHandler(kinectSensorChooser1_KinectSensorChanged);
    
            }
    
            void kinectSensorChooser1_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                var oldSensor = (KinectSensor)e.OldValue;
    
                if (oldSensor != null)
                {
                    StopKinect(oldSensor);
                }
                var newSensor = (KinectSensor)e.NewValue;
                if (newSensor == null)
                {
                    return;
                }
    
                newSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(newSensor_AllFramesReady);
    
                // turn on features that you need
                newSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                newSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                newSensor.SkeletonStream.Enable();
                
                try
                {
                    newSensor.Start();
                }
                catch (System.IO.IOException)
                {
                    //This happens if another app is using the kinect
                    kinectSensorChooser1.AppConflictOccurred();
    
                }
            }
    
            void newSensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
            {
                using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
                {
                    if (colorFrame == null)
                    {
                        return;
                    }
    
                    byte[] pixels = new byte[colorFrame.PixelDataLength];
                    colorFrame.CopyPixelDataTo(pixels);
    
                    int stride = colorFrame.Width * 4;
                    image1.Source =
                        BitmapSource.Create(colorFrame.Width, colorFrame.Height,
                        96, 96, PixelFormats.Bgr32, null, pixels, stride);
                }
    
            }
            void StopKinect(KinectSensor sensor)
            {
                if (sensor != null)
                {
                    sensor.Stop();
                    sensor.AudioSource.Stop();
                }
            }
    
            private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                StopKinect(kinectSensorChooser1.Kinect);
    
            }
        }
    }