C# graphics drawlines draws broken lines

只愿长相守 提交于 2021-02-20 04:28:09

问题


I am drawing ECG graphs using C# Graphics. I draw the curve using drawlines method. however line joints look broken. I tried all available options of smoothing mode and capstyle, none helps. here is sample graph1 and sample graph2

code is below:

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

回答1:


To avoid broken lines especially when the lines are drawn at sharp angles you need to choose the right values for these Properties:

p.MiterLimit = p.Width * 1.25f;
p.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;

The MiterLimit has a default of 10f, which is way to big for thin lines! The LineJoin also has a default (Miter) that does not help.

You should also experiment a little with the MiterLimit value (keep in the range of the Pen's width) and maybe also with your Pen's width itself; do note that Pen.Width is a float so you could raise it to 1.25 or so..

If you are actually talking about the smudgy look at some spots, this is due to anti-aliasing; usually a good thing, but for crisper lines turn it off for your Graphics object:

e.Graphics.SmoothingMode =  System.Drawing.Drawing2D.SmoothingMode.None

The LineCaps are only for the start- and the end-point of the lines-sequence, so they do not matter much for your graph.




回答2:


You are drawing short lines. This is why you are getting that result. Instead of DrawLines(), try to use DrawCurve().

g.DrawCurve(p, ps.ToArray());


来源:https://stackoverflow.com/questions/24105757/c-sharp-graphics-drawlines-draws-broken-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!