C# GDI+曲线绘制问题

发布于 2024-10-10 01:37:18 字数 1972 浏览 3 评论 0原文

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

I'm trying to draw a series of connected segments, but the curved segments seem to produce an artifact, whereby the outer side of the curve is not smooth at all, but very jagged. This is part of a GIS program I am making.

For these lines, the line itself needs to be quite wide, as this represents the range of data that can be collected on this line for the GIS data. There also has to be an area directly under the line where no data is collected. This also can be wide, but not as wide as the main line.

I have done this using a graphics path, which I then widen and use as a clipping region to block the area directly under the line. I then draw the actual line. The sample code below does this, with made up values for ease of regenerating.

This works fine with straight lines, but with curved lines there are very irregular shapes on the outside of the curves. I have no idea why this happens.

Any ideas would be much appreciated, cheers,

Greg

I made this sample code using a basic form with a picturebox and a button on it, whereby when I clicked the button it would execute this method:

    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();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

败给现实 2024-10-17 01:37:18

尝试对排除区域使用单独的 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();
                }
            }
        }

    }
}

Try to use a separate GraphicsPath for excluded region:

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();
                }
            }
        }

    }
}
若沐 2024-10-17 01:37:18

在您的 Graphics 实例上正确设置平滑模式。看看这里< /a>.

Set the smoothing mode properly on your Graphics instance. Take a look here.

巡山小妖精 2024-10-17 01:37:18

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

Try setting the CompositingQuality, the InterpolationMode and the SmoothingMode properties to increase the quality of your Graphics object:

using(Graphics g = Graphics.FromImage(image))
{
    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    //...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文