/// <summary> /// Local msthod for firing the PreProcessBodyFrame event /// </summary> /// <param name="e"></param> protected virtual void OnPreProcessBodyFrame(PreProcessBodyFrameEventArgs e) { if (PreProcessBodyFrame != null) PreProcessBodyFrame(this, e); }
async void bodyReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e) { CalculateFPS(); try { // grab color frame var colorFrame = colorReader.AcquireLatestFrame(); if (colorFrame != null) { // ColorFrame is IDisposable using (colorFrame) { BodyFrame frame = e.FrameReference.AcquireFrame(); if (frame != null) { using (frame) { SensorStatus = SensorStatus.Active; if (this.bodies == null) { this.bodies = new Body[kinectSensor.BodyFrameSource.BodyCount]; } // The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array. // As long as those body objects are not disposed and not set to null in the array, // those body objects will be re-used. frame.GetAndRefreshBodyData(this.bodies); FrameDescription frameDescription = colorFrame.FrameDescription; // verify data and write the new color frame data to the display bitmap if ((frameDescription.Width == this.videoImageSource.PixelWidth) && (frameDescription.Height == this.videoImageSource.PixelHeight)) { await DisplayColorFrame(colorFrame); } if (DrawBodies == true) { using (DrawingContext dc = this.drawingGroup.Open()) { // Draw a transparent background to set the render size // get size of color space dc.DrawRectangle(Brushes.Transparent, null, new Rect(0.0, 0.0, frameDescription.Width, frameDescription.Height)); for (int i = 0; i < bodies.Length; i++) { Body body = bodies[i]; if (body.IsTracked) { this.DrawClippedEdges(body, dc); var args = new PreProcessBodyFrameEventArgs(frame, bodies); OnPreProcessBodyFrame(args); IReadOnlyDictionary<JointType, Joint> joints = body.Joints; foreach (JointType jointType in joints.Keys) { this.Joints[(int)jointType] = joints[jointType]; } Point[] jointPoints = new Point[joints.Count]; // convert the Joint points to depth (display) space foreach (JointType jointType in joints.Keys) { ColorSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToColorSpace(joints[jointType].Position); jointPoints[(int)jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y); } this.DrawBody(Joints, jointPoints, dc); this.DrawHand(body.HandLeftState, jointPoints[(int)JointType.HandLeft], dc); this.DrawHand(body.HandRightState, jointPoints[(int)JointType.HandRight], dc); DrawHead(body, dc); OnBodyTracked(new BodyTrackedEventArgs(body, i, dc)); } } // prevent drawing outside of our render area this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, colorWidth, colorHeight)); } } else { for (int i = 0; i < bodies.Length; i++) { Body body = bodies[i]; if (body.IsTracked) { OnBodyTracked(new BodyTrackedEventArgs(body, i, null)); } } } } } } } } catch (Exception ex) { // ignore if the frame is no longer available var msg = ex.Message; } }