C# GraphicsPath 绘图问题
我正在尝试在程序中绘制 GraphicsPath,但它似乎存在问题,它会向路径添加随机尖峰。道路越宽,情况似乎变得越糟。我编写了一些可以复制这个问题的测试代码。
此代码需要一个包含 PictureBox 的表单(PictureBox 尺寸至少为 630 x 1050)和一个按钮。代码如下:
private void button1_Click(object sender, EventArgs e)
{
drawSomeLines();
pictureBox1.Refresh();
}
private void drawSomeLines()
{
//initialise the plot area:
Bitmap image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.BackgroundImage = image;
Graphics g = Graphics.FromImage(image);
//create a graphics path:
GraphicsPath gPath = new GraphicsPath();
//add sections to the path:
gPath.AddLine(587.310059F, 29.2261658F, 229.974731F, 668.2402F);
gPath.AddArc(new RectangleF(203.177338F,560.3876F,421.357F,421.357F), -(90 - 299.21382700000413F), -1.532426F);
gPath.AddArc(new RectangleF(203.177368F,560.3876F,421.357F,421.357F), -(90 - 297.72672132554612F), -1.53240252F);
gPath.AddLine(224.740067F,678.2186F, 76.6899643F,979.773865F);
//draw the path 3 times, at different widths:
g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(100, Color.Blue)), 80), gPath);
g.DrawPath(new Pen(new SolidBrush(Color.Blue), 40), gPath);
g.DrawPath(new Pen(new SolidBrush(Color.Red), 2), gPath);
}
我在这里以不同的宽度绘制了 3 次路径,这表明问题在较大的宽度下变得更糟。
有谁知道为什么会发生这种情况,以及我如何防止它?任何想法将不胜感激。
干杯,
格雷格
I am trying to draw a GraphicsPath in my program, but it seems to have issues where it adds random spikes to the path. This seems to get worse the wider the path is. I have made some test code which can replicate this problem.
This code required a form with a PictureBox in it (PictureBox dimensions at least 630 x 1050), and a single button. The code is then as follows:
private void button1_Click(object sender, EventArgs e)
{
drawSomeLines();
pictureBox1.Refresh();
}
private void drawSomeLines()
{
//initialise the plot area:
Bitmap image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.BackgroundImage = image;
Graphics g = Graphics.FromImage(image);
//create a graphics path:
GraphicsPath gPath = new GraphicsPath();
//add sections to the path:
gPath.AddLine(587.310059F, 29.2261658F, 229.974731F, 668.2402F);
gPath.AddArc(new RectangleF(203.177338F,560.3876F,421.357F,421.357F), -(90 - 299.21382700000413F), -1.532426F);
gPath.AddArc(new RectangleF(203.177368F,560.3876F,421.357F,421.357F), -(90 - 297.72672132554612F), -1.53240252F);
gPath.AddLine(224.740067F,678.2186F, 76.6899643F,979.773865F);
//draw the path 3 times, at different widths:
g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(100, Color.Blue)), 80), gPath);
g.DrawPath(new Pen(new SolidBrush(Color.Blue), 40), gPath);
g.DrawPath(new Pen(new SolidBrush(Color.Red), 2), gPath);
}
I have drawn the path here 3 times at different widths, which shows how the problem gets worse at larger widths.
Does anyone know why this happens, and how I might prevent it? Any ideas would be much appreciated.
Cheers,
Greg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您现在可能已经弄清楚了这一点,但这就是您的线条中出现“尖峰”的原因:
当您将第二个圆弧添加到图形路径对象时,其起点出现在您添加的第一个圆弧的端点“之前” 。发生这种情况时,图形路径必须回溯以开始绘制第二个圆弧,最终会出现一些非常尖锐且形状奇怪的角,尤其是在线宽较大的情况下。
如果线条中有间隙,graphicspath 可以很好地填充它们,但如果有重叠,结果就不太好。
如果您将这行代码更改
为:
它会删除 2 个弧段中的重叠,然后图形路径可以填充间隙并绘制平滑的段,而不会产生产生“尖峰”的不良角效果。
从显示的角度来看,在线中有间隙比有任何重叠要好得多。
You have probably figured this out by now, but this is why you are getting the 'spikes' in your line:
When you add the second arc to your graphicspath object, its startpoint occurs 'before' the endpoint of the first arc that you added. When this happens, the graphics path has to backtrack to start drawing the second arc and you end up with some very sharp and oddly shaped corners, especially at large line widths.
If you have gaps in your line, graphicspath does a good job of filling them in, but if you have overlap, the results are not so good.
If you change this line of code:
to this:
it removes the overlap in your 2 arc segments and the graphicspath can then fill in the gap and draw a smooth segment without the undesirable corner effects that produce the 'spikes'.
From a display standpoint, it is much better to have gaps in your line, rather than having any overlaps.