Graphics.DrawLines的尖角

时间:2018-12-09 21:53:32

标签: c# winforms graphics gdi+

当我使用Graphics.DrawLines绘制点序列时,粗线的角(在示例中为30)看起来非常尖锐,如下图所示。拐角尖峰的实际外观似乎是随机的。使用DrawCurve代替DrawLines没有帮助。

enter image description here

我检查了点序列是否包含相邻的相同点,希望它可能只是线的数学定义不正确。但是,不,第一个列表显示了右上角附近的点,第二个列表显示了右下角笔画结束附近的点。

enter image description here

enter image description here

public class Stroke
{
    Pen pen;
    List<Point> points;

    public readonly Color Color;
    public readonly int Id;
    public readonly float PenWidth;

    public Stroke(int id, Color c, float penWidth)
    {
        points = new List<Point>();
        Id = id;
        Color = c; 
        PenWidth = penWidth;
        pen = new Pen(Color, PenWidth);
        pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
        pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
    }

    public void Draw(Graphics graphics)
    {
        if ((points.Count < 2) || (graphics == null))
        {
            return;
        }

        graphics.DrawLines(pen, (Point[]) points.ToArray());
        //graphics.DrawCurve(pen, (Point[]) points.ToArray());
    }

    public void Add(Point pt)
    {
        if (points.Count > 0 && points[points.Count-1] == pt) return;
        points.Add(pt);
    }

    public void Dispose()
    {
        pen.Dispose();
    }
}

以下列方式使用

BufferedGraphics finalBuffer;
width = Screen.PrimaryScreen.Bounds.Width;
height = Screen.PrimaryScreen.Bounds.Height;

using (Graphics graphics = CreateGraphics())
{
    finalBuffer = BufferedGraphicsManager.Current.Allocate(graphics, new Rectangle(0,0,width,height));
}

// ...

Graphics g = finalBuffer.Graphics;
stroke.Draw(g);
stroke.Dispose();

其中finalBuffer是OnPaint内部呈现给e.Graphics的内容。

0 个答案:

没有答案