C# 图形drawlines绘制折线

msu*_*zer 1 c# graphics antialiasing

我正在使用 C# Graphics 绘制心电图。我使用画线方法绘制曲线。然而线接头看起来断了。我尝试了平滑模式和 capstyle 的所有可用选项,但没有任何帮助。这是示例图 1示例图 2

代码如下:

private void DrawCurve(Graphics g, cPoint[] data)
{
    List<Point> ps = new List<Point>();

    for (int i = 0; i < data.Length - 1; i++)
    {
        int x = data[i].x;
        int y = data[i].y;

        if (x > 0 && x < (Width))
        {
            ps.Add(new Point(x, y));
        }
        else if (x > Width)
        {
            using (Pen p = new Pen(Color.Yellow))
            {
                if (ps.Count > 0)
                {
                    g.DrawLines(p, ps.ToArray());
                    ps.Clear();
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

TaW*_*TaW 6

为了避免断线,特别是当以锐角绘制线时,您需要为这些属性选择正确的值:

p.MiterLimit = p.Width * 1.25f;
p.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
Run Code Online (Sandbox Code Playgroud)

MiterLimit默认值为 10f,对于细线来说太大了!LineJoin 还有一个默认值(斜接),但没有帮助。

您还应该尝试一下MiterLimit值(保持在笔的宽度范围内),也许还可以尝试一下您的Pen宽度本身;请注意,Pen.Width是 a float,因此您可以将其提高到 1.25 左右。

如果您实际上谈论的是某些地方的污迹,这是由于抗锯齿造成的;通常是一件好事,但为了获得更清晰的线条,请为您的 Graphics 对象关闭它:

e.Graphics.SmoothingMode =  System.Drawing.Drawing2D.SmoothingMode.None
Run Code Online (Sandbox Code Playgroud)

它们LineCaps仅适用于线序列的起点和终点,因此它们对于您的图表来说并不重要。