我用钢笔绘制一个带有凹凸曲线的形状。
我需要在图形中填充颜色,我该怎么做?
这是我的代码:
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), });
我找到了填充路径,但我不知道如何使用它。
答案 0 :(得分:6)
使用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;
}
产地: