我在事件 sensor_DepthFrameReady 中创建了一个线程,但是当我执行它时,在函数 SetImage1Frame 的第 8 行(Depths[depth]++;)上抛出异常并抛出此异常:
索引超出了数组的范围。(深度 = -1)
出了什么问题?我认为函数(SetImage1Frame)执行了不止一次,但我只在这个方法(sensor_DepthFrameReady)中使用了这个函数!!
void sensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
{
short[] pixels;
Thread th;
using (frame=e.OpenDepthImageFrame())
{
if (frame!=null)
{
pixels = new short[frame.PixelDataLength];
frame.CopyPixelDataTo(pixels);
th = new Thread(() =>
SetImage1Frame(pixels));
th.Start();
}
}
}
int[] Depths=new int[4096];
int depth=0;
double chartBarWidth;
void SetImage1Frame(short[] dpixels)
{
int maxValue = 0;
for (int i = 0; i < dpixels.Length; i++)
{
depth = dpixels[i] >> DepthImageFrame.PlayerIndexBitmaskWidth;
if (depth > 0)
{
Depths[depth]++;
}
}
for (int i = 0; i < Depths.Length; i++)
{
maxValue=Math.Max(maxValue,Depths[i]);
}
for (int i = 0; i < Depths.Length; i++)
{
if (Depths[i] > 0)
{
stackPanel1.Dispatcher.Invoke(new Action(() =>
{
Rectangle r = new Rectangle();
stackPanel1.Children.Add(r);
}));
}
}
image1.Dispatcher.BeginInvoke(new Action(() =>
{
WrbitmapImage.WritePixels(rect, dpixels, stride, 0);
}));
}