Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 使用DrawingContext绘制曲线_Wpf_Drawingcontext - Fatal编程技术网

Wpf 使用DrawingContext绘制曲线

Wpf 使用DrawingContext绘制曲线,wpf,drawingcontext,Wpf,Drawingcontext,我正在尝试创建一个自定义控件来使用WPF绘制曲线 我创建了一个从控件继承的控件: public class Courbe : Control { private static readonly CultureInfo CI = new CultureInfo("en-US"); private Pen _p = new Pen(); public Courbe() { Points = new List<Point>(); }

我正在尝试创建一个自定义控件来使用WPF绘制曲线

我创建了一个从控件继承的控件:

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
}
但在性能方面,它比windows窗体中的Graphics.DrawCurve慢2倍左右。有没有办法让它更快


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

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

改变

PathSegment ps = new LineSegment(scaledPoint, false);


非常感谢。我明天会试试的,我没注意到那很好!处理时间与DrawCurve大致相同。
PathSegment ps = new LineSegment(scaledPoint, false);
PathSegment ps = new LineSegment(scaledPoint, true);