Search code examples
c#drawingstylus-pen

Changing stroke color dynamically


I am using stylus input to draw lines in a canvas. I want to change the color of the stroke with the pen pressure. so I used:

DrawingAttributes dattribute = new DrawingAttributes();
inkcan.EditingMode = InkCanvasEditingMode.Ink;

if (stylusInput.pressureFactor < 0.5)
dattribute.Color = Colors.Red;
else
dattribute.Color = Colors.Blue;

inkcan.DefaultDrawingAttributes = dattribute;

but I have found that the color changes only when I lift off and retouch the pen to tablet surface. I am not sure how to fix that problem.

Any help would be greatly appreciated.


Solution

  • Look at this question: InkCanvas Eraser

    In the MSDN it states:

    If you change the EraserShape, the cursor rendered on the InkCanvas is not updated until the next EditingMode change.

    The effect you are experiencing might be caused by the EditingMode being changed when you pull the pen off the canvas and put it back down.

    If so, you could toggle the EditingMode property as I suggested in the linked answer.

    EDIT

    Have a look at this a 3rd down it says:

    Off course, life is never as simple as that, so there is one more little issue to handle. Apparently, the InkCanvas uses different renderers for the final result compared to while the strokes are being drawn. To show a transparency based on the pressure while the drawing action is still in progress, we need to use the protected property called DyamicRenderer which gets/sets the object used to render the strokes on a drawing context while the stroke is being drawn. This rendering object must be a descendent of DynamicRenderer. All you need to do here is override the OnDraw method and change the brush that is used. When you assign a new value to this property, the InkCanvas actually changes an internal 'PlugIn list' which is called whenever data is entered using the stylus.

    This might be it.