question

HoneyBee-2519 avatar image
0 Votes"
HoneyBee-2519 asked HoneyBee-2519 edited

Is there a way to set FitToCurve in InkCanvas' DynamciRender?

I'm working on an app to draw a lot of strokes in an InkCanvas surface.

My big problem is while user draws a stroke(mouseDown and MouseMove), it doesn't look smooth:

Only after user had finished to draw this stroke(MouseUp) it gets smooth because I set InkCanvas to FitToCurve. But there is a kind of flick.

Can I use CustomDynamicRender to allow smooth processing while the user is drawing?

As a result, we want the result while drawing and the final reflected result (Stroke) to be the same.

With FitToCurve settings applied

/// Updated

Here is my DynamicRenderer's code.

Doing this source will add lines over and over again.

Is there a way to erase and redraw the existing line to keep the stroke neatly one line?

And this method is obviously very poorly performing.

Is there a better way?

 class CustomDynamicRenderer : DynamicRenderer
     {
         //[ThreadStatic]
         //static private Brush brush = null;
         //
         //[ThreadStatic]
         //static private Pen pen = null;
    
         private Point prevPoint;
    
         StylusPointCollection stylusPointsColl;
    
         DrawingAttributes inkDA = new DrawingAttributes();
    
         Stroke stroke;
    
         public CustomDynamicRenderer() : base()
         {
             inkDA.IgnorePressure = true;
             inkDA.Width = 1;
             inkDA.Height = 1;
             inkDA.Color = Colors.Red;
             inkDA.FitToCurve = true;
    
             Matrix matrix = new Matrix(1, 0, 0, 1, 0, 0);
             matrix.Rotate(45);
                
             inkDA.StylusTipTransform = matrix;
         }
    
         protected override void OnStylusUp(RawStylusInput rawStylusInput)
         {
             base.OnStylusUp(rawStylusInput);
         }
    
         protected override void OnStylusDown(RawStylusInput rawStylusInput)
         {
             // Allocate memory to store the previous point to draw from.
             prevPoint = new Point(double.NegativeInfinity, double.NegativeInfinity);
             stylusPointsColl ??= new StylusPointCollection();
             stylusPointsColl.Clear();
                
             base.OnStylusDown(rawStylusInput);
         }
    
         InkPresenter ip = new InkPresenter();
    
         protected override void OnDraw(DrawingContext drawingContext,
                                        StylusPointCollection stylusPoints,
                                        Geometry geometry, Brush fillBrush)
         {
             foreach(var stylusPoint in stylusPoints)
             {
                 Point pt = (Point)stylusPoint;
                 Vector v = Point.Subtract(prevPoint, pt);
    
                 if(v.Length > 25)
                 {
                     drawingContext.Dispatcher.Invoke(new Action(() =>
                     {
                         stylusPointsColl.Add(new StylusPoint(stylusPoint.X, stylusPoint.Y, 0.5f));
    
                         stroke ??= new Stroke(stylusPointsColl, inkDA);
    
                         stroke.StylusPoints = stylusPointsColl;
                         stroke.StylusPoints = stroke.GetBezierStylusPoints();
    
                         stroke.Draw(drawingContext, inkDA);
                            
                         prevPoint = pt;
                     }));
                 }
             }
         }
     }
windows-wpf
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@HoneyBee-2519
I made a sample which using InkCanvas, but I can't reproduce your not smooth problem. Could you provide some code snippet for me to make a sample to reproduce your problem? By the way, what is your project target platofrm?

0 Votes 0 ·

I am developing in C# (Net Framework 4.8) in visual studio 2019 and developing a platform that works on both x86 and x64.

0 Votes 0 ·
 <Grid>
         <InkCanvas x:Name="inkCanvas1" />
     </Grid>

this.inkCanvas1.DefaultDrawingAttributes.FitToCurve = true;

The first image is dragging with the mouse.

Lines are not drawn smoothly in DynamicRender.

The second image is the mouse dragged.

Because the FitToCurve property is true
The lines are corrected smoothly.

I want the line to be corrected smoothly while the line is being drawn (while dragging the mouse). (Same effect as FitToCurve)


![96732-image.png]


96702-image.png


0 Votes 0 ·
image.png (16.5 KiB)
image.png (17.0 KiB)

@DaisyTian-MSFT

The content has been updated.

I'm developing a WPF program that runs on Windows 10, and I'm using C#.

The .NET Framework is 4.8.

Overridden DynamicRenderer's OnDraw method.

Draws on the DrawingContext based on the collected stylus points.

I would like to use DrawingAttributes at this time.
"
inkDA.IgnorePressure = true;
inkDA.Width = 1;
inkDA.Height = 1;
inkDA.Color = Colors.Red;
inkDA.FitToCurve = true;
"

And I hope FitToCurve is applied while drawing so the lines are drawn smoothly.

When all drawing is done, the line is calibrated with the InkCanvas' FitToCurve property.

I hope to be corrected while drawing.

As a result
The final goal is to make corrections while drawing so that no additional corrections occur after drawing is finished.

0 Votes 0 ·

0 Answers