C#图形绘制线绘制虚线 [英] C# graphics drawlines draws broken lines

查看:141
本文介绍了C#图形绘制线绘制虚线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#图形绘制ECG图形.我使用画线法绘制曲线.但是,线接头看起来坏了.我尝试了平滑模式和capstyle的所有可用选项,没有帮助.这是示例图1

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

代码如下:

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;

MiterLimit的默认值为10f,对于细线来说这是很大的方法!LineJoin还具有默认设置(斜接),该默认设置无济于事.

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.

您还应该使用 MiterLimit 值(保持在笔的宽度范围内)实验,也许还可以使用 Pen 的宽度本身;请注意,请注意 Pen.Width float ,因此您可以将其提高到1.25左右.

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..

如果您实际上是在谈论某些地方的污迹,那是由于抗锯齿;通常是一件好事,但是对于更清晰的线条,请为您的Graphics对象将其关闭:

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

LineCaps 仅用于线条序列的起点和终点,因此它们对图形没有太大影响.

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

这篇关于C#图形绘制线绘制虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆