我正在用笔用 drawcurve 绘制一个形状。
我需要在该图形中填充颜色,我该怎么做?
这是我的代码:
Pen p1 = new Pen(Color.Red);
Graphics g1 = panel1.CreateGraphics();
g1.DrawCurve(p1, new Point[] { new Point(470, 470), new Point(430, 440), new Point(400, 480), new Point(470, 560), });
Graphics g2 = panel1.CreateGraphics();
g2.DrawCurve(p1, new Point[] { new Point(470, 470), new Point(510, 440), new Point(540, 480), new Point(470, 560), });
我找到了填充路径,但我不知道如何使用它。
最佳答案
使用 GraphicsPath 类。您可以使用 Graphics.FillPath 填充它并在必要时使用 Graphics.DrawPath 绘制轮廓。并确保只在 Paint 事件处理程序中绘制,无论您使用 CreateGraphics() 绘制什么都不会在面板自行重绘时持续很长时间。
using System.Drawing.Drawing2D;
...
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
panelPath = new GraphicsPath();
panelPath.AddCurve(new Point[] { new Point(470, 470), new Point(430, 440), new Point(400, 480), new Point(470, 560), });
panelPath.AddCurve(new Point[] { new Point(470, 470), new Point(510, 440), new Point(540, 480), new Point(470, 560), });
panel1.Paint += new PaintEventHandler(panel1_Paint);
}
void panel1_Paint(object sender, PaintEventArgs e) {
e.Graphics.TranslateTransform(-360, -400);
e.Graphics.FillPath(Brushes.Green, panelPath);
e.Graphics.DrawPath(Pens.Red, panelPath);
}
GraphicsPath panelPath;
}
产生:
关于c# - 如何在钢笔绘制的形状中填充颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773655/