Search code examples
.netuwpstrokeinkcanvas

How to modifying InkStrokes of an InkCanvas in .net?


I'm developing an universal windows platform App and want to modify the attribute of Ink Strokes that exist in Ink Canvas, All i know is i can access as below:

IReadOnlyList<InkStroke> = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();

Any example of editing size or color of these things and redrawing them ?

Another question is how can i have an event on drawing that strokes? for example how can i draw an exact same stroke on another location on same Ink Canvas in real time?


Solution

  • You can redraw your Ink Strokes that exist in Ink Canvas by setting new InkDrawingAttributes for InkStroke.DrawingAttributes.

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        InkDrawingAttributes attr = new InkDrawingAttributes();
        attr.Color = Colors.Red;
        attr.IgnorePressure = true;
        attr.PenTip = PenTipShape.Circle;
        attr.Size = new Size(4, 10);
        attr.PenTipTransform = Matrix3x2.CreateRotation((float)(70 * Math.PI / 180));
        IReadOnlyList<InkStroke> InkStrokeList = MyInk.InkPresenter.StrokeContainer.GetStrokes();
        foreach (InkStroke temp in InkStrokeList)
        {
            temp.DrawingAttributes = attr; 
        }          
    }
    

    The event of InkPresenter.StrokesCollected occurs when one or more ink strokes are processed (wet to dry) by the application thread, and then copy exist InkStrokes to new point of Ink Canvas. You can draw an exact same stroke on another location on same Ink Canvas in real time by using following code.

     public MainPage()
     {
         this.InitializeComponent();
         MyInk.InkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Mouse | CoreInputDeviceTypes.Touch;
         MyInk.InkPresenter.StrokesCollected += InkPresenter_StrokesCollected;
     }
    
     private void InkPresenter_StrokesCollected(InkPresenter sender, InkStrokesCollectedEventArgs args)
     {
         foreach (var temp in args.Strokes)
         {
             temp.Selected = true;
             sender.StrokeContainer.CopySelectedToClipboard();
             sender.StrokeContainer.PasteFromClipboard(new Point(100, 100));
         }
     }