Reputation: 374
I am drawing a shape with drawcurve with pen.
I need to fill the color in that graphics how can I do that?
This is my code:
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), });
I found fill path but I don't know how to use that.
Upvotes: 4
Views: 7547
Reputation: 941307
Use the GraphicsPath class. You can draw it filled with Graphics.FillPath and draw the outline, if necessary, with Graphics.DrawPath. And be sure to only ever draw in the Paint event handler, whatever you draw with CreateGraphics() is not going to last long when the panel redraws itself.
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;
}
Produces:
Upvotes: 6