C#GDI +曲线绘图问题

时间:2010-12-29 13:55:04

标签: c# .net-2.0 gdi+

我正在尝试绘制一系列连接的线段,但弯曲的线段似乎会产生一个伪影,因此曲线的外侧根本不光滑,但非常锯齿状。这是我正在制作的GIS计划的一部分。

对于这些行,行本身需要非常宽,因为这表示可以在此行上为GIS数据收集的数据范围。还必须有一个直接在线下没有收集数据的区域。这也可以很宽,但不如主线宽。

我使用图形路径完成此操作,然后我将其扩展并用作剪切区域以阻挡直线下方的区域。然后我画出实际的线。下面的示例代码执行此操作,其中包含易于重新生成的值。

这适用于直线,但曲线的曲线外部有非常不规则的形状。我不知道为什么会这样。

任何想法都会受到高度赞赏,欢呼,

格雷格

我使用带有图片框和按钮的基本表单制作了这个示例代码,当我点击按钮时,它将执行此方法:

    private void drawCurvedLine()
    {
        //initialise the plot area:
        Bitmap image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        pictureBox1.BackgroundImage = image;

        Graphics g = Graphics.FromImage(image);

        //the width of the pen represents the width of a sonar swathe:
        Pen widePen = new Pen(new SolidBrush(Color.FromArgb(80, Color.Blue)), 50);

        PointF[] points = new PointF[4];

        //first straight:
        points[0] = new PointF(287.284149F,21.236269F);
        points[1] = new PointF(183.638443F,406.936249F);

        //second straight:
        points[2] = new PointF(130.842773F, 515.574036F);
        points[3] = new PointF(-1950.91321F, 3491.868F);

        //graphics path for the line:
        GraphicsPath gPath = new GraphicsPath();

        gPath.AddLine(points[0], points[1]);
        gPath.AddArc(new RectangleF(-445.464447F,3.84924316F,640.067444F,640.067444F), -(90 - 105.0412369999982F), 10.8775282F);
        gPath.AddArc(new RectangleF(-445.464417F, 3.84915161F, 640.067444F, 640.067444F), -(90 - 115.91811484539707F), 10.8775091F);
        gPath.AddLine(points[2], points[3]);

        //widen the line to the width equal to what the fish will not be able to see:
        gPath.Widen(new Pen(Color.White, 10));

        //now exclude that widened line from the main graphics:
        g.ExcludeClip(new Region(gPath));

        //draw the swathe line:
        g.DrawPath(widePen, gPath);

        //reset the clipping for the next line:
        g.ResetClip();
    }

3 个答案:

答案 0 :(得分:2)

尝试对排除的区域使用单独的GraphicsPath

private void drawCurvedLine()
{
    //initialise the plot area:
    Bitmap image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    pictureBox1.BackgroundImage = image;

    using(Graphics g = Graphics.FromImage(image))
    {
        PointF[] points = new PointF[4];

        //first straight:
        points[0] = new PointF(287.284149F, 21.236269F);
        points[1] = new PointF(183.638443F, 406.936249F);

        //second straight:
        points[2] = new PointF(130.842773F, 515.574036F);
        points[3] = new PointF(-1950.91321F, 3491.868F);

        //graphics path for the line:
        using(GraphicsPath gPath = new GraphicsPath())
        {
            gPath.AddLine(points[0], points[1]);
            gPath.AddArc(new RectangleF(-445.464447F, 3.84924316F, 640.067444F, 640.067444F), -(90 - 105.0412369999982F), 10.8775282F);
            gPath.AddArc(new RectangleF(-445.464417F, 3.84915161F, 640.067444F, 640.067444F), -(90 - 115.91811484539707F), 10.8775091F);
            gPath.AddLine(points[2], points[3]);

            //widen the line to the width equal to what the fish will not be able to see:
            using(GraphicsPath innerPath = (GraphicsPath)gPath.Clone())
            {
                using(Pen pen = new Pen(Color.White, 10))
                {
                    innerPath.Widen(pen);
                }

                //now exclude that widened line from the main graphics:
                using(Region reg = new Region(innerPath))
                {
                    g.ExcludeClip(reg);

                    //draw the swathe line:
                    //the width of the pen represents the width of a sonar swathe:
                    using(Pen widePen = new Pen(new SolidBrush(Color.FromArgb(80, Color.Blue)), 50))
                    {
                        g.DrawPath(widePen, gPath);
                    }

                    //reset the clipping for the next line:
                    g.ResetClip();
                }
            }
        }

    }
}

答案 1 :(得分:0)

Graphics实例上正确设置平滑模式。看看here

答案 2 :(得分:0)

尝试设置CompositingQuality,InterpolationMode和SmoothingMode属性以提高Graphics对象的质量:

using(Graphics g = Graphics.FromImage(image))
{
    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    //...
}