4

When I use inkcanvas1.EditingMode is InkCanvasEditingMode.EraseByPoint it is erasing and also StrokeErased event firing.
but when I erase stroke through inkcanvas1.Strokes.Erase(); function then StrokeErased event is not firing.
then how can I identify which stroke erased and which strokes are newly created.
consider on my inkcanvas thousands of strokes.
so I'm not able to maintain every added and removed strokes. is there any other event or any solution.

I have a below sample WPF Inkcanvas code
XAML Code

<Window x:Class="MyWhiteBoard.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyWhiteBoard"
        mc:Ignorable="d"
        WindowState="Maximized"
        Title="MainWindow" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <InkCanvas x:Name="inkcanvas1" Grid.Row="0" StrokeErased="inkcanvas1_StrokeErased" MouseMove="inkcanvas1_MouseMove"></InkCanvas>
        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Width="100" Height="50" Background="WhiteSmoke" Content="Draw" Click="drawInkClick"></Button>
            <Button Width="100" Height="50" Background="WhiteSmoke" Content="Erase by Point" Click="EraseByPointsClick"></Button>
            <Button Width="100" Height="50" Background="WhiteSmoke" Content="Stroke Erase" Click="StrokeEraseClick"></Button>
            <Button Width="100" Height="50" Background="WhiteSmoke" Content="clear all" Click="clearAllClick"></Button>
        </StackPanel>
    </Grid>
</Window>

Code Behind C# code

public partial class MainWindow : Window
    {
        private bool IsStrokeEraseCalled = false;
        public MainWindow()
        {
            InitializeComponent();
        }
        private void inkcanvas1_StrokeErased(object sender, RoutedEventArgs e)
        {
            // why this event is not calling when I use  
            //inkcanvas1.Strokes.Erase
        }
        private void EraseByPointsClick(object sender, RoutedEventArgs e)
        {
            inkcanvas1.EditingMode = InkCanvasEditingMode.EraseByPoint;
        }
        private void StrokeEraseClick(object sender, RoutedEventArgs e)
        {
            inkcanvas1.EditingMode = InkCanvasEditingMode.Select;
            IsStrokeEraseCalled = !IsStrokeEraseCalled;
        }
        private void clearAllClick(object sender, RoutedEventArgs e)
        {
            inkcanvas1.Strokes.Clear();
        }
        private void inkcanvas1_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsStrokeEraseCalled)
            {
                Point currentPoint = e.GetPosition((IInputElement)sender);
                List<Point> enumrator = new List<Point>();
                enumrator.Add(new Point(currentPoint.X, currentPoint.Y));
                StylusShape EraseShape = (StylusShape)new RectangleStylusShape(100, 100, 0);
                inkcanvas1.Strokes.Erase(enumrator, EraseShape);
            }
        }

        private void drawInkClick(object sender, RoutedEventArgs e)
        {
            inkcanvas1.EditingMode = InkCanvasEditingMode.Ink;
        }
    }
Maahi
  • 327
  • 1
  • 10
Sandeep Jadhav
  • 815
  • 1
  • 10
  • 27

3 Answers3

2

Replace your StrokeEraseClick event with following code:

private void StrokeEraseClick(object sender, RoutedEventArgs e)
    {
        inkcanvas1.EditingMode = InkCanvasEditingMode.EraseByPoint;
        inkcanvas1.EraserShape = new RectangleStylusShape(100, 100);
        var editMode = inkcanvas1.EditingMode;
        inkcanvas1.EditingMode = InkCanvasEditingMode.None;
        inkcanvas1.EditingMode = editMode;
    }      

It will leave the current mode to InkCanvasEditingMode.EraseByPoint and allow you to erase defined area stokes. StrokeErased event will be fired every time you erase strokes using this approach.

Maahi
  • 327
  • 1
  • 10
  • 1
    Hello Maahi, you are erasing the strokes from InkCanvasEditingMode.EraseByPoint. you have increased the size of inkcanvas default erasing shape. I have asked question inkcanvas1_StrokeErased event was not firing after doing below lines from code. StylusShape EraseShape = (StylusShape)new RectangleStylusShape(100, 100, 0); inkcanvas1.Strokes.Erase(enumrator, EraseShape); – Sandeep Jadhav Oct 23 '20 at 08:16
  • 1
    Inkcanvas StrokeErased event fires automatically only if EditingMode is set to EraseByPoint . Hope this answer your question. Please refer following link [link](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.inkcanvas.strokeerased?view=netcore-3.1) – Maahi Oct 23 '20 at 08:24
  • in my question at first line I already mentioned event was firing in EraseByPointMode but it is not firing when I Erase stroke through inkcanvas1.Strokes.Erase(enumrator, EraseShape); – Sandeep Jadhav Oct 23 '20 at 08:28
2

try this

  public MainWindow()
    {
        InitializeComponent();
        inkcanvas1.Strokes.StrokesChanged += Strokes_StrokesChanged;
    }

    private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
    {
        StrokeCollection newlyAddedStroke =  e.Added;
        StrokeCollection oldRemovedStroke = e.Removed;
    }
  • StrokesChanged event is immediately calling when I even erased single point from a stroke. but I want an event it should fire after stroke completely erasing. – Sandeep Jadhav Oct 23 '20 at 14:40
2
  1. When you erase stroke through inkcanvas1.Strokes.Erase(); function then the StrokeErased event will not fire by default.
  2. If you want to maintain added and removed strokes then you can handle it in inkcanvas1.Strokes.StrokesChanged event.
  3. Or you should be in EraseByPoint Mode and use statement
    inkcanvas1.EraserShape = new RectangleStylusShape(100, 100);
  4. If you have bulk strokes on your inkcanvas then for maintaining every strokecollection every time is heavy then you have choice to add unique id to every stroke using ProperyData property.
funie200
  • 3,688
  • 5
  • 21
  • 34