简体   繁体   English

C#图形绘制线绘制虚线

[英]C# graphics drawlines draws broken lines

I am drawing ECG graphs using C# Graphics. 我正在使用C#图形绘制ECG图形。 I draw the curve using drawlines method. 我使用画线法绘制曲线。 however line joints look broken. 但是,线接头看起来坏了。 I tried all available options of smoothing mode and capstyle, none helps. 我尝试了平滑模式和capstyle的所有可用选项,没有任何帮助。 here is sample graph1 and sample graph2 这是示例graph1示例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();
                }
            }
        }
    }
}

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! MiterLimit的默认值为10f,对于细线来说这是很大的方法! The LineJoin also has a default (Miter) that does not help. LineJoin还具有默认设置(斜接),该默认设置无济于事。

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; 你也应该尝试用一下MiterLimit值(保持钢笔的宽度的范围),也许还与你的Pen的宽度本身; do note that Pen.Width is a float so you could raise it to 1.25 or so.. 注意Pen.Width是一个float因此您可以将其提高到1.25左右。

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: 通常是一件好事,但是对于更清晰的线条,请为您的Graphics对象将其关闭:

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. LineCaps仅适用于线序的起点和终点,因此它们对图形没有太大影响。

You are drawing short lines. 您正在画短线。 This is why you are getting that result. 这就是为什么您得到该结果的原因。 Instead of DrawLines() , try to use DrawCurve() . 尝试使用DrawCurve()代替DrawLines ()

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

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

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