C# Kinect,警告:未释放ImageFrame实例

C# Kinect,警告:未释放ImageFrame实例,c#,wpf,kinect,C#,Wpf,Kinect,我是Kinect的新手。我试图使用骨架跟踪,但我一直收到警告“警告:未处理ImageFrame实例”。你知道有什么解决办法吗 这是我的密码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.W

我是Kinect的新手。我试图使用骨架跟踪,但我一直收到警告“警告:未处理ImageFrame实例”。你知道有什么解决办法吗

这是我的密码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using Microsoft.Kinect;
using Coding4Fun.Kinect.Wpf;
using System.Diagnostics;
using System.IO;

namespace KinectSkeleton
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        bool closing = false;
        const int skeletonCount = 6;
        Skeleton[] allSkeletons = new Skeleton[skeletonCount];

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            myKinectSensorChooser.KinectSensorChanged += new DependencyPropertyChangedEventHandler(myKinectSensorChooser_KinectSensorChanged);
        }

        void myKinectSensorChooser_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            KinectSensor oldSensor = (KinectSensor)e.OldValue;
            if (oldSensor != null)
            {
                oldSensor.Stop();
                oldSensor.AudioSource.Stop();
            }

            KinectSensor mySensor = (KinectSensor)e.NewValue;
            if (mySensor == null)
                return;

            mySensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);
            mySensor.ColorStream.Enable();
            mySensor.SkeletonStream.Enable();
            mySensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(mySensor_AllFramesReady);

            try
            {
                mySensor.Start();
                Debug.WriteLine("Starting Sensor .....");
                Debug.WriteLine("The Current Elevation Angle is: " + mySensor.ElevationAngle.ToString());
                mySensor.ElevationAngle = 0;
            }
            catch (System.IO.IOException)
            {
                //another app is using Kinect
                myKinectSensorChooser.AppConflictOccurred();
            }
        }

        void mySensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            if (closing)
                return;

            byte[] depthImagePixels;
            DepthImageFrame depthFrame = e.OpenDepthImageFrame();
            if (depthFrame == null)
                return;

            depthImagePixels = GenerateDepthImage(depthFrame);
            int stride = depthFrame.Width*4;
            image1.Source =
                BitmapSource.Create(depthFrame.Width, depthFrame.Height,
                96, 96, PixelFormats.Bgr32, null, depthImagePixels, stride);


            //Get a skeleton
            Skeleton first = GetFirstSkeleton(e);
            if (first == null)
                return;

            Debug.WriteLine("Head Position is : " + first.Joints[JointType.Head].ToString());

            depthFrame.Dispose();

        }

        private byte[] GenerateDepthImage(DepthImageFrame depthFrame)
        {
            //get the raw data from the frame with the depth for every pixel
            short[] rawDepthData = new short[depthFrame.PixelDataLength];
            depthFrame.CopyPixelDataTo(rawDepthData);

            //use frame to create the image to display on-screen
            //frame contains color information for all pixels in image
            //Height x Width x 4 (Red, Green, Blue, empty byte)
            Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4];

            //hardcoded locations to Blue, Green, Red (BGR) index positions       
            const int BlueIndex = 0;
            const int GreenIndex = 1;
            const int RedIndex = 2;
            int player, depth;

            //loop through all distances
            //pick a RGB color based on distance
            for (int depthIndex = 0, colorIndex = 0;
                depthIndex < rawDepthData.Length && colorIndex < pixels.Length;
                depthIndex++, colorIndex += 4)
            {
                //get the player (requires skeleton tracking enabled for values)
                player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask;

                //gets the depth value
                depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth;

                if (player > 0)
                {
                    pixels[colorIndex + BlueIndex] = Colors.Gold.B;
                    pixels[colorIndex + GreenIndex] = Colors.Gold.G;
                    pixels[colorIndex + RedIndex] = Colors.Gold.R;
                }
                else
                {
                    pixels[colorIndex + BlueIndex] = Colors.Green.B;
                    pixels[colorIndex + GreenIndex] = Colors.Green.G;
                    pixels[colorIndex + RedIndex] = Colors.Green.R;
                }
            }
            //depthFrame.Dispose();
            return pixels;
        }

        Skeleton GetFirstSkeleton(AllFramesReadyEventArgs e)
        {
            using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame())
            {
                if (skeletonFrameData == null)
                {
                    return null;
                }
                skeletonFrameData.CopySkeletonDataTo(allSkeletons);

                //get the first tracked skeleton
                Skeleton first = (from s in allSkeletons
                                  where s.TrackingState == SkeletonTrackingState.Tracked
                                  select s).FirstOrDefault();

                return first;
            }
        }

        void StopKinect(KinectSensor sensor)
        {
            if (sensor != null)
            {
                if (sensor.IsRunning)
                {
                    sensor.ElevationAngle = 0;
                    sensor.Stop();
                    if (sensor.AudioSource != null)
                    {
                        sensor.AudioSource.Stop();
                    }
                }
            }
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            closing = true;
            Debug.WriteLine("Closing window...");
            StopKinect(myKinectSensorChooser.Kinect);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用Microsoft.Kinect;
使用Coding4Fun.Kinect.Wpf;
使用系统诊断;
使用System.IO;
命名空间KinectSkeleton
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
布尔关闭=假;
常量int skeletonCount=6;
骨架[]所有骨架=新骨架[skeletonCount];
已加载私有无效窗口(对象发送器、路由目标)
{
myKinectSensorChooser.KinectSensorChanged+=新的依赖项PropertyChangedEventHandler(myKinectSensorChooser\U KinectSensorChanged);
}
void myKinectSensorChooser\u KinectSensorChanged(对象发送方,DependencyPropertyChangedEventArgs e)
{
KinectSensor oldSensor=(KinectSensor)e.OldValue;
如果(旧传感器!=null)
{
oldSensor.Stop();
oldSensor.AudioSource.Stop();
}
KinectSensor mySensor=(KinectSensor)e.NewValue;
if(mySensor==null)
返回;
mySensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);
mySensor.ColorStream.Enable();
mySensor.SkeletonStream.Enable();
mySensor.AllFramesReady+=新事件处理程序(mySensor_AllFramesReady);
尝试
{
mySensor.Start();
Debug.WriteLine(“启动传感器…”);
WriteLine(“当前仰角为:“+mySensor.ElevationAngle.ToString());
mySensor.ElevationAngle=0;
}
捕获(System.IO.IOException)
{
//另一个应用程序正在使用Kinect
myKinectSensorChooser.AppConflictOccurrend();
}
}
void mySensor_allframesrady(对象发送方,allframesradyeventargs e)
{
如果(结束)
返回;
字节[]深度图像像素;
DepthImageFrame depthFrame=e.OpenDepthImageFrame();
if(depthFrame==null)
返回;
depthImagePixels=GenerateDepthImage(depthFrame);
int stride=深度帧。宽度*4;
图1.来源=
创建(depthFrame.Width、depthFrame.Height、,
96,96,PixelFormats.Bgr32,null,深度图像像素,步幅);
//弄到骨架
骨架优先=GetFirstSkeleton(e);
if(first==null)
返回;
Debug.WriteLine(“头部位置为:”+first.Joints[JointType.Head].ToString());
depthFrame.Dispose();
}
专用字节[]GeneratedEventHimage(DepthImageFrame depthFrame)
{
//从帧中获取每个像素深度的原始数据
short[]rawDepthData=新的short[depthFrame.PixelDataLength];
CopyPixelDataTo(rawDepthData);
//使用frame创建要在屏幕上显示的图像
//框架包含图像中所有像素的颜色信息
//高度x宽度x 4(红色、绿色、蓝色、空字节)
字节[]像素=新字节[depthFrame.Height*depthFrame.Width*4];
//将位置硬编码为蓝色、绿色、红色(BGR)索引位置
const int BlueIndex=0;
常数int GreenIndex=1;
常数int RedIndex=2;
int播放器,深度;
//环行所有距离
//基于距离拾取RGB颜色
对于(int-depthIndex=0,colorIndex=0;
depthIndex>DepthImageFrame.PlayerIndexBitmaskWidth;
如果(玩家>0)
{
像素[colorIndex+BlueIndex]=Colors.Gold.B;
像素[colorIndex+GreenIndex]=Colors.Gold.G;
像素[colorIndex+RedIndex]=Colors.Gold.R;
}
其他的
{
像素[colorIndex+BlueIndex]=Colors.Green.B;
像素[colorIndex+GreenIndex]=Colors.Green.G;
像素[colorIndex+RedIndex]=Colors.Green.R;
}
}
//depthFrame.Dispose();
返回像素;
}
骨架GetFirstSkeleton(所有框架ReadyEventArgs e)
{
使用(SkeletonFrame skeletonFrameData=e.OpenSkeletonFrame())
{
if(skeletonFrameData==null)
{
返回null;
}
skeletonFrameData.CopySkeletonDataTo(所有骨架);
//获取第一个跟踪的骨架
骨架优先=(从所有骨架中的s开始)
其中s.TrackingState==SkeletonTrackingState.Tracked
选择s).FirstOrDefault();
先返回;
}
}
       //Get a skeleton
        Skeleton first = GetFirstSkeleton(e);
        if (first == null)
            return; // Here if first skeleton is null then you are returning without disposing depthFrame frame.
  using(DepthImageFrame depthFrame = e.OpenDepthImageFrame())
  {
        if (depthFrame == null)
            return;

        depthImagePixels = GenerateDepthImage(depthFrame);
        int stride = depthFrame.Width*4;
        image1.Source =
            BitmapSource.Create(depthFrame.Width, depthFrame.Height,
            96, 96, PixelFormats.Bgr32, null, depthImagePixels, stride);


        //Get a skeleton
        Skeleton first = GetFirstSkeleton(e);
        if (first == null)
            return;

        Debug.WriteLine("Head Position is : " + first.Joints[JointType.Head].ToString());
     }