WPF InkCanvas Smooth Stroke

HoneyBee 186 Reputation points
2022-03-03T02:57:19.207+00:00

I am working on a C# WPF project using NET5.

I got a lot of help from MSDN, and I am very grateful.

The core function of my program utilizes InkCanvas.

Specifically,
Drawing on the wall with an infrared pen
The infrared sensor communicates this to the window as a mouse event.

And my program draws the content recognized as a mouse event in this process on InkCanva.

Instead of a mouse or a stylus pen, it can be said that the infrared pen and the mouse are processed through image processing.

I am trying out various infrared products.
There is a problem because each product has slightly different characteristics.

I want to draw soft lines,
Lines are not smooth as the InkCanvas displays even the slightest tremors.

Is there any way to correct this tremor?

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-03-07T07:13:32.13+00:00

    @HoneyBee , Based on my further research, I suggest that you could create stroke data from mouse input on a Canvas overlay over an InkCanvas.

    Here is a code example you could refer to.

    xaml:

    <Grid>  
            <InkCanvas x:Name="inkCanvas" IsHitTestVisible="False"/>  
            <Canvas Background="Transparent"  
                MouseLeftButtonDown="Canvas_MouseLeftButtonDown"  
                MouseLeftButtonUp="Canvas_MouseLeftButtonUp"  
                MouseMove="Canvas_MouseMove"/>  
      
        </Grid>  
    

    c# Code:

    private Stroke stroke;  
            private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
            {  
                var pos = e.GetPosition(inkCanvas);  
                var points = new StylusPointCollection  
        {  
            new StylusPoint(pos.X, pos.Y)  
        };  
      
                stroke = new Stroke(points, inkCanvas.DefaultDrawingAttributes);  
                inkCanvas.Strokes.Add(stroke);  
            }  
      
            private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)  
            {  
                stroke = null;  
            }  
      
            private void Canvas_MouseMove(object sender, MouseEventArgs e)  
            {  
                if (stroke != null)  
                {  
                    var pos = e.GetPosition(inkCanvas);  
                    stroke.StylusPoints.Add(new StylusPoint(pos.X, pos.Y));  
                }  
            }  
    

    Hope this could hepl you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.