void hTracker_onNewData(HandTracker hTracker) { if (!hTracker.isValid) return; using (HandTrackerFrameRef frame = hTracker.readFrame()) { if (!frame.isValid) return; lock (image) { using (OpenNIWrapper.VideoFrameRef depthFrame = frame.DepthFrame) { if (image.Width != depthFrame.FrameSize.Width || image.Height != depthFrame.FrameSize.Height) image = new Bitmap(depthFrame.FrameSize.Width, depthFrame.FrameSize.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); } using (Graphics g = Graphics.FromImage(image)) { g.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), image.Size)); foreach (GestureData gesture in frame.Gestures) if (gesture.isComplete) hTracker.startHandTracking(gesture.CurrentPosition); if (frame.Hands.Length == 0) g.DrawString("Raise your hand", SystemFonts.DefaultFont, Brushes.White, 10, 10); else foreach (HandData hand in frame.Hands) { if (hand.isTracking) { Point HandPosEllipse = new Point(); PointF HandPos = hTracker.ConvertHandCoordinatesToDepth(hand.Position); HandPosEllipse.X = (int)HandPos.X - 5; HandPosEllipse.Y = (int)HandPos.Y - 5; g.DrawEllipse(new Pen(Brushes.White, 5), new Rectangle(HandPosEllipse, new Size(5, 5))); } } g.Save(); } } this.Invoke(new MethodInvoker(delegate() { fps = ((1000000 / (frame.Timestamp - lastTime)) + (fps * 4)) / 5; lastTime = frame.Timestamp; this.Text = "Frame #" + frame.FrameIndex.ToString() + " - Time: " + frame.Timestamp.ToString() + " - FPS: " + fps.ToString(); pb_preview.Image = image.Clone(new Rectangle(new Point(0, 0), image.Size), System.Drawing.Imaging.PixelFormat.Format24bppRgb); })); } }
// ReSharper disable once ParameterHidesMember private void HandTrackerOnNewData(HandTracker handTracker) { if (!handTracker.IsValid) { return; } HandTrackerFrameRef frame = handTracker.ReadFrame(); if (frame == null || !frame.IsValid) { return; } lock (this.image) { using (VideoFrameRef depthFrame = frame.DepthFrame) { if (this.image.Width != depthFrame.FrameSize.Width || this.image.Height != depthFrame.FrameSize.Height) { this.image = new Bitmap( depthFrame.FrameSize.Width, depthFrame.FrameSize.Height, PixelFormat.Format24bppRgb); } } using (Graphics g = Graphics.FromImage(this.image)) { g.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), this.image.Size)); foreach (GestureData gesture in frame.Gestures) { if (gesture.IsComplete) { handTracker.StartHandTracking(gesture.CurrentPosition); } } if (frame.Hands.Length == 0) { g.DrawString("Raise your hand", SystemFonts.DefaultFont, Brushes.White, 10, 10); } else { foreach (HandData hand in frame.Hands) { if (hand.IsTracking) { Point handPosEllipse = new Point(); PointF handPos = handTracker.ConvertHandCoordinatesToDepth(hand.Position); handPosEllipse.X = (int)handPos.X - 5; handPosEllipse.Y = (int)handPos.Y - 5; g.DrawEllipse(new Pen(Brushes.White, 5), new Rectangle(handPosEllipse, new Size(5, 5))); } } } g.Save(); } } this.Invoke( new MethodInvoker( delegate { this.fps = ((1000000 / (frame.Timestamp - this.lastTime)) + (this.fps * 4)) / 5; this.lastTime = frame.Timestamp; this.Text = @"Frame #" + frame.FrameIndex + @" - Time: " + frame.Timestamp + @" - FPS: " + this.fps; this.pb_preview.Image = this.image.Clone( new Rectangle(new Point(0, 0), this.image.Size), PixelFormat.Format24bppRgb); frame.Release(); })); }