开发者

Thick Line Drawing problem in C#.NET

I wanted to draw thick lines using Graphics.Lines() method. But it looks like the API has some bugs. If you try to render the user control with the following code, you would get weird looking image. I was wondering if there is some smoothing mode or something similar that could take care of th开发者_运维知识库is line drawing glitch.

private void UserControl1_Paint(object sender, PaintEventArgs e)
    {
        int n = 100;
        Point[] points = new Point[n];

        double x = 2;
        int y = 50;

        for (int i = 0; i < n; i++)
        {
            Point p = new Point();
            p.X = 200 + (int)(i * x);
            p.Y = 200 + (int)(Math.Sin(i * 0.2) * y);
            points[i] = p;
        }

        Pen pen = new Pen(new SolidBrush(Color.Blue));
        //Pen pen = new Pen(new LinearGradientBrush(new Point(0, 0), new Point(0, 100), Color.Black, Color.Red));
        pen.Width = 200;
        e.Graphics.DrawLines(pen, points);
    }


You see the effect of GDI+ trying to draw end-caps on the line. That's not going to come to a good end with such a thick pen. About what you'd imagine from daVinci painting the Mona Lisa with a broom. Fix:

        Pen pen = new Pen(new SolidBrush(Color.Blue));
        pen.EndCap = System.Drawing.Drawing2D.LineCap.Square;
        pen.StartCap = System.Drawing.Drawing2D.LineCap.Square;

Or draw a polygon instead so that GDI+ has a better idea what is front and back:

        e.Graphics.DrawPolygon(pen, points);

Well, it doesn't look like a devil anymore. Keep the line width proportional to the details in the line.


Here is the result of your code drawing using a pen of width 200 (pixels):

Thick Line Drawing problem in C#.NET

And here it is at a width of 2:

Thick Line Drawing problem in C#.NET

The pen width property is usually pixels, but it is based on the Graphics object's PageUnit property (itself a GraphicsUnit property). Check to make sure you've set these values to what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜