我正在使用InkCanvas在WPF中编写基本的图形编辑器。我做了一些自定义形状(继承自Stroke)。当我在InkCanvas上绘制wen线时,我将获得第一个点和最后一个点并划一条线。效果很好,但是现在我不喜欢默认的“笔触”,所以我决定重写DynamicRenderer以实时渲染线条。

问题是,DynamicRenderer从原点到笔划的每个点画线,而我显然不希望这样做,因为它使"fan"陷入了困境。

有我的自定义代码,如果可能,我正在寻找仅从原点到最后一点画线的解决方案。

class LineRenderer : DynamicRenderer
{
    private Point firstPoint;
    private Pen pen = new Pen(new SolidColorBrush(Colors.Gray),1);

    public LineRenderer()
    {
        firstPoint = new Point(double.PositiveInfinity, double.PositiveInfinity);
    }

    protected override void OnStylusDown(RawStylusInput rawStylusInput)
    {
        firstPoint = new Point(rawStylusInput.GetStylusPoints().First().ToPoint().X, rawStylusInput.GetStylusPoints().First().ToPoint().Y);
        base.OnStylusDown(rawStylusInput);
    }

    protected override void OnDraw(DrawingContext drawingContext,
                                   StylusPointCollection stylusPoints,
                                   Geometry geometry, Brush fillBrush)
    {
        drawingContext.DrawLine(pen, firstPoint, stylusPoints.First().ToPoint());
    }

    protected override void OnStylusUp(RawStylusInput rawStylusInput)
    {
        firstPoint = new Point(double.PositiveInfinity, double.PositiveInfinity);
        base.OnStylusUp(rawStylusInput);
    }
}

最佳答案

这太迟了。为了避免在绘制笔划时出现“扇形”,请尝试以下操作:

protected override void OnDraw(DrawingContext drawingContext,
                               StylusPointCollection stylusPoints,
                               Geometry geometry, Brush fillBrush)
        {
            if (!_isManipulating)
            {
                _isManipulating = true;
                StylusDevice currentStylus = Stylus.CurrentStylusDevice;
                this.Reset(currentStylus, stylusPoints);
            }
            _isManipulating = false;

            var pen = new Pen(brush, 2);
            drawingContext.DrawLine(pen, startPoint,stylusPoints.First().ToPoint());
       }


protected override void OnStylusDown(RawStylusInput rawStylusInput)
        {
            StylusPointCollection y = rawStylusInput.GetStylusPoints();
            startPoint = (Point)y.First();

            // Allocate memory to store the previous point to draw from.
            prevPoint = new Point(double.NegativeInfinity, double.NegativeInfinity);
            base.OnStylusDown(rawStylusInput);
        }


这里的窍门是使用DynamicRenderer.Reset,它可以:


  清除当前笔划上的渲染并重新绘制。


重绘将重新输入OnDraw方法,因此_isManipulating提供了一个简单的标志来停止循环。

关于c# - WPF InkCanvas:使用DynamicRenderer绘制线条,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17676185/

10-10 05:19