简体   繁体   English

WPF InkCanvas:使用DynamicRenderer绘制线条

[英]WPF InkCanvas: Draw line with DynamicRenderer

I'm writing basic graphic editor in WPF using InkCanvas. 我正在使用InkCanvas在WPF中编写基本的图形编辑器。 I've made some custom shapes (inherited from Stroke). 我做了一些自定义形状(继承自Stroke)。 When I draw line wen on InkCanvas, I take first and last point and make a line. 当我在InkCanvas上画线wen时,我要先画最后一条线。 That works fine, but now I don't like the default "pen stroke" so i decided to rewrite DynamicRenderer to render line in real time. 效果很好,但是现在我不喜欢默认的“笔触”,所以我决定重写DynamicRenderer以实时渲染线。

Problem is, that DynamicRenderer draws line from origin point to every point of stroke and i obviously don't want that, becouse it makes "fan" insted of line. 问题是,DynamicRenderer从原点到笔划的每个点都画线,而我显然不希望那样做,因为这会使“ fan”迷住了线条。

There is my custom code and I'm looking for solution to draw line from origin point to last point only, if it is possible. 有我的自定义代码,如果可能,我正在寻找仅从原点到最后一点画线的解决方案。

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);
    }
}

This is very much late. 这太迟了。 In order to avoid the "fan" while the stroke is being drawn, try this: 为了避免在绘制笔划时出现“扇形”,请尝试以下操作:

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);
        }

The trick here is to use DynamicRenderer.Reset , which: 这里的技巧是使用DynamicRenderer.Reset ,它是:

Clears rendering on the current stroke and redraws it. 清除当前笔划上的渲染并重新绘制。

The redraw will reenter the OnDraw method, so _isManipulating provides a simple flag to stop looping. 重绘将重新输入OnDraw方法,因此_isManipulating提供了一个简单的标志来停止循环。

what i think in your ondraw function your first point is fixed so it always take it as origin so you can try this for continous drawing. 我认为在您的ondraw函数中,您的第一点是固定的,因此始终将其作为原点,以便可以尝试进行连续绘制。

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

it will update your firstpoint if you get same point exception put a check on it ( or you can define anew point name as previous point and initially make it equal to first point and keep it updating in your ondraw method as i did with first point.. 如果遇到相同的点异常,它将更新您的第一点(或者可以将新的点名称定义为先前的点,并使其最初与第一点相等,然后像我对第一点所做的那样在ondraw方法中将其更新。 。

and if you want direct line from first point to last point you can called you on draw method after getting last point from onstylusup method ..hope this might help you.. 如果要从第一点到最后一点的直线,可以在从onstylusup方法获得最后一点后在draw方法上调用您。希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM