使用DrawingContext绘制曲线

时间:2014-05-05 13:08:26

标签: wpf drawingcontext

我尝试制作自定义控件以使用WPF绘制曲线。

我制作了一个继承自Control的控件:

public class Courbe : Control {

    private static readonly CultureInfo CI = new CultureInfo("en-US");
    private Pen _p = new Pen();

    public Courbe() {
        Points = new List<Point>();
    }
    static Courbe() {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Courbe), new FrameworkPropertyMetadata(typeof(Courbe)));
    }

    protected override void OnRender(DrawingContext drawingContext) {
        base.OnRender(drawingContext);
        EcritureEvenement.loggerEvenement("verif point...", TypesEvenements.DEBUG, "");
        if (Points.Count <= 1) return;
        EcritureEvenement.loggerEvenement("plus d'un point", TypesEvenements.DEBUG, "");
        OffsetX = (int)Math.Ceiling(Width / 11);
        OffsetY = (int)Math.Ceiling(Height / 11);

        double differenceX = MaxX - MinX;
        double differenceY = MaxY - MinY;

        double rapportTailleDifferenceX = (Width) / differenceX;
        double rapportTailleDifferenceY = (Height) / differenceY;

        double xMinLigne = rapportTailleDifferenceX * MinX + OffsetX;
        double xMaxLigne = rapportTailleDifferenceX * MaxX + OffsetX;
        double yMinLigne = (Height - (rapportTailleDifferenceY * (MinY - MinY)) - OffsetY);
        double yMaxLigne = (Height - (rapportTailleDifferenceY * (MaxY - MinY)) - OffsetY);
        Point debutAxeX = new Point(xMinLigne, yMinLigne);
        Point finAxeX = new Point(xMaxLigne, yMinLigne);
        Point debutAxeY = new Point(xMinLigne, yMinLigne);
        Point finAxeY = new Point(xMinLigne, yMaxLigne);
        drawingContext.DrawLine(_p, debutAxeX, finAxeX);
        drawingContext.DrawLine(_p, debutAxeY, finAxeY);

        int taillePolice = (int)Math.Floor(Width / 80);

        double dizieme = differenceY / 10;
        for (int i = 0; i < 10; i++) {
            float pointGraduation = (float)(Height - ((dizieme * i) * rapportTailleDifferenceY) - OffsetY);

            FormattedText texte = new FormattedText(dizieme * i + MinY + "", CI, FlowDirection.LeftToRight, new Typeface("Verdana"), taillePolice, Brushes.Black);
            drawingContext.DrawText(texte, new Point(0, pointGraduation - taillePolice));
            Point p1 = new Point(xMinLigne - 5, pointGraduation);
            Point p2 = new Point(xMinLigne + 5, pointGraduation);
            drawingContext.DrawLine(_p, p1, p2);
        }

        PathFigure pf = new PathFigure();

        foreach (Point p in Points) {
            double x1 = (p.X * rapportTailleDifferenceX) + OffsetX;
            double y1 = Height - ((p.Y - MinY) * rapportTailleDifferenceY) - OffsetY;
            Point scaledPoint = new Point((float)x1, (float)y1);
            PathSegment ps = new LineSegment(scaledPoint, false);
            pf.Segments.Add(ps);
        }
        Geometry g = new PathGeometry(new[]{pf});
        drawingContext.DrawGeometry(Brushes.Aqua, new Pen(Brushes.Blue, 5), g);
    }

    #region Properties
    public List<Point> Points { get; set; }
    public int MinX { get; set; }
    public int MaxX { get; set; }
    public int MinY { get; set; }
    public int MaxY { get; set; }
    public int OffsetX { get; set; }
    public int OffsetY { get; set; }

    private Color _penColor;

    public Color PenColor {
        get { return _penColor; }
        set {
            _p = new Pen(new SolidColorBrush(value), 5);
            _penColor = value;
        }
    }

    #endregion
}

我的问题是,这条线似乎不是用笔画的,只是用画笔画(如果我把null而不是画笔.Aqua没有出现)。

谢谢你

编辑:如果我使用DrawLine而不是DrawGeometry它正在工作:

Point ancienPoint = new Point(-1, -1);
foreach (Point p in Points) {

    double x1 = (p.X * rapportTailleDifferenceX) + OffsetX;
    double y1 = Height - ((p.Y - MinY) * rapportTailleDifferenceY) - OffsetY;
    Point scaledPoint = new Point((float)x1, (float)y1);
    if (ancienPoint.X != -1 && ancienPoint.Y != -1) {
        drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.Blue), 1), ancienPoint, scaledPoint);
    }
    ancienPoint = scaledPoint;
}

但是关于性能,它比Windows窗体中的Graphics.DrawCurve慢约2倍。有什么方法可以让它更快?

Edit2:我没有尝试使用本机控件,因为当我在Windows窗体(图表)中尝试时,控件无法处理该过程(每隔约50毫秒绘制约5000行)

1 个答案:

答案 0 :(得分:2)

问题是,当您调用isStroked构造函数时,您将false设置为LineSegment

更改

PathSegment ps = new LineSegment(scaledPoint, false);

PathSegment ps = new LineSegment(scaledPoint, true);