【问题标题】:C# GDI+ curve drawing issueC# GDI+ 曲线绘制问题
【发布时间】:2011-06-01 01:56:08
【问题描述】:

我正在尝试绘制一系列连接的线段,但弯曲的线段似乎会产生伪影,即曲线的外侧根本不平滑,而是非常参差不齐。这是我正在制作的 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();
    }

【问题讨论】:

  • 添加一些代码是个好主意,但您可以编辑您现有的问题,而不是发布新的副本。
  • 在旁注中,您正在泄漏资源。 Pen、Graphics 和 Path 对象应在离开此方法之前进行处置。
  • 对不起,我是这个网站的新手,我没有意识到我可以编辑我的旧问题。

标签: c# .net-2.0 gdi+


【解决方案1】:

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

【讨论】:

  • 通常我确实设置了平滑模式,但没有用于此示例代码。我尝试了所有选项,但仍然出现同样的问题。
【解决方案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();
                }
            }
        }

    }
}

【讨论】:

    【解决方案3】:

    尝试设置 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;
        //...
    }
    

    【讨论】:

    • 谢谢,但这仍然会产生相同的效果。尽管使用 Max 上面的建议,我还是能够解决它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 2011-04-01
    • 2022-01-13
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多