C#GDI +曲线图问题 [英] C# GDI+ curve drawing issue

查看:196
本文介绍了C#GDI +曲线图问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一系列相连的线段,但是弯曲的线段似乎会产生伪像,因此曲线的外侧根本不平滑,但锯齿状。这是我正在制作的GIS程序的一部分。

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.

对于这些行,该行本身必须足够宽,因为这表示可以收集的数据范围在这条线上获取GIS数据。线下还必须有一个区域,其中不收集任何数据。

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


推荐答案

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

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

    }
}

这篇关于C#GDI +曲线图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆