title: c# wpf multiple line drawing on canvas with kinect sensor
body: <p>So first of all let me show you what I want to accomplish:</p>
  
  <p><a href="http://www.youtube.com/watch?NR=1&amp;feature=endscreen&amp;v=FdwZEepJxJ4" rel="nofollow">http://www.youtube.com/watch?NR=1&amp;feature=endscreen&amp;v=FdwZEepJxJ4</a></p>
  
  <p>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 </p>
  
  <p>layoutGrid.Children.Add(myLine)</p>
  
  <p>This works, but not the way I want.</p>
  
  <p>I was thinking I could use drawingContext to draw lines with:</p>
  
  <p>drawingContext.DrawLine(drawPen, pointprev, point1);</p>
  
  <p>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?</p>
  
  <p>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? </p>
  
  <pre><code>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));
          }
  </code></pre>