Search code examples
c#wpfdrawkinectlines

c# wpf multiple line drawing on canvas with kinect sensor


So first of all let me show you what I want to accomplish:

http://www.youtube.com/watch?NR=1&feature=endscreen&v=FdwZEepJxJ4

There the guy is drawing a long curved line using kinect. I need to make something similar. I had several ideas with just simple code like

layoutGrid.Children.Add(myLine)

This works, but not the way I want.

I was thinking I could use drawingContext to draw lines with:

drawingContext.DrawLine(drawPen, pointprev, point1);

The problem is that by using this drawing method the lines I draw are not kept on the screen. It's like the line is always redrawn. How do I keep my lines?

By the way I also don't understand why the skeleton drawn by the same method gets updated and we don't need to clear the previous lines in the image?

private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            // Create the drawing group we'll use for drawing
            this.drawingGroup = new DrawingGroup();

            // Create an image source that we can use in our image control
            this.imageSource = new DrawingImage(this.drawingGroup);

            // Display the drawing using our image control
            Image2.Source = this.imageSource;

            ************Some other code here*******
         }


 private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            *****************Some code to get skeleton data here********

            using (DrawingContext dc = this.drawingGroup.Open())
            {

                if (skeletons.Length != 0)
                {
                    foreach (Skeleton skel in skeletons)
                    {

                        if (skel.TrackingState == SkeletonTrackingState.Tracked)
                        {

                            this.DrawBonesAndJoints(skel, dc);                            
                        }

                    }
                }
            }           
        }


private void DrawBonesAndJoints(Skeleton skeleton, DrawingContext drawingContext)
        {
            ***********************Some code to draw skeleton***************

            Joint righthand = skeleton.Joints[JointType.HandRight];

            if (init == true)
            {
                pointprev = SkeletonPointToScreen(righthand.Position);
                init = false;
                return;
            }


            Point point1 = SkeletonPointToScreen(righthand.Position);

            Pen drawPen = this.trackedBonePen;

            drawingContext.DrawLine(drawPen, pointprev, point1);


            pointprev = point1;

        }

private Point SkeletonPointToScreen(SkeletonPoint skelpoint)
        {
            // Convert point to depth space.  
            // We are not using depth directly, but we do want the points in our 640x480 output resolution.
            DepthImagePoint depthPoint = this.sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(skelpoint, DepthImageFormat.Resolution640x480Fps30);
            return new Point(depthPoint.X, depthPoint.Y);
        }


        private void DrawBone(Skeleton skeleton, DrawingContext drawingContext, JointType jointType0, JointType jointType1)
        {
            Joint joint0 = skeleton.Joints[jointType0];
            Joint joint1 = skeleton.Joints[jointType1];          

            Pen drawPen = this.trackedBonePen;   

            drawingContext.DrawLine(drawPen, this.SkeletonPointToScreen(joint0.Position),       this.SkeletonPointToScreen(joint1.Position));
        }

Solution

  • How do I keep my lines?

    You would have to either use a Polyline or draw a PathGeometry or a StreamGeometry into a DrawingContext by DrawGeometry. The geometry would have to contain one or more polyline segments.