How to get just the resulting strokes when erasing by point on InkCanvas?

1.1k views Asked by At

I don't understand. Lets say I draw an "S" on an InkCanvas.

The OnStrokeCollected event will fire. Inside the OnStrokeCollected event, I send the stroke to an InkAnalyzer:

analyzer.AddStroke(e.Stroke)

Now I erase-by-point a center point of the "S".

The OnStrokeErasing event fires. Now I can remove the original "S" from the InkAnalzyer:

analyzer.RemoveStroke(e.Stroke)

But, I now have two strokes. The top and the bottom of the original "S". Since I now have two strokes, how do I get each of these "new" strokes to add back to the InkAnalyzer (having already removed the original composite stroke "S") without removing the entire previous stroke collection and adding anew the new strokecollection ?

I sincerely appreciate any ideas.

1

There are 1 answers

0
Alan Wayne On

The only idea I could come up with is posted below. I would surely appreciate a better solution.

XAML

  <ink:CustomInkCanvas x:Name="inkcanvas"  Strokes="{Binding Strokes}" /> 

ViewModel

        strokes = new StrokeCollection();
        (strokes as INotifyCollectionChanged).CollectionChanged += (sender, e) =>
        {
            if (isErasingByPoint == true)
            {
                if (e.OldItems != null && e.OldItems.Count > 0)
                {
                    foreach (Stroke s in e.OldItems)
                    {
                    }
                }

                if (e.NewItems != null && e.NewItems.Count > 0)
                {
                    foreach (Stroke s in e.NewItems)
                    {
                    }

                }
            }
            isErasingByPoint = false;

        };


    public void OnStrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
    {
        // OnStrokeCollected event occurs AFTER StrokeCollection.CollectionChanged event.
        analyzer.AddStroke(e.Stroke);
    }

    public void OnStrokeErasing(object sender,  InkCanvasStrokeErasingEventArgs e)
    {
        // When erasingbypoint, OnStrokeErasing event occurs BEFORE StrokeCollection.CollectionChanged event.
        // if erasing by stroke, the StrokeCollection.CollectionChanged event is not called.
        if (EditingMode == InkCanvasEditingMode.EraseByPoint)
            isErasingByPoint = true;
        else
            analyzer.RemoveStroke(e.Stroke);
    }

Hope this is of use to somebody.