如何填充笔绘制的形状中的颜色

alt*_*raz 4 c# graphics c#-4.0

我正在画一个带笔的曲线形状.

我需要在图形中填充颜色,我该怎么做?

这是我的代码:

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), });
Run Code Online (Sandbox Code Playgroud)

我找到了填充路径,但我不知道如何使用它.

Han*_*ant 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;
    }
Run Code Online (Sandbox Code Playgroud)

生产:

在此输入图像描述