如何为GraphicsPath应用外边界?我尝试了以下代码,但将边框应用于单个矩形而不是整个路径。
我的预期输出屏幕截图如下。
我尝试过的可运行示例:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += new System.EventHandler(this.Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath graphicsPath = new GraphicsPath();
Rectangle rect1 = new Rectangle(20, 20, 100, 30);
Rectangle rect2 = new Rectangle(20, 50, 40, 20);
graphicsPath.StartFigure();
graphicsPath.AddRectangle(rect1);
graphicsPath.AddRectangle(rect2);
graphicsPath.CloseAllFigures();
e.Graphics.FillPath(Brushes.LightGreen, graphicsPath);
e.Graphics.DrawPath(Pens.DarkGreen, graphicsPath);
}
}
请建议我如何实现预期的输出。提前致谢。
使用多边形而不是矩形来绘制形状可获得所需的结果:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += new System.EventHandler(this.Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath graphicsPath = new GraphicsPath();
Point[] pts = new Point[] { new Point(20, 20),
new Point(120, 20),
new Point(120, 50),
new Point(60, 50),
new Point(60, 70),
new Point(20, 70) };
graphicsPath.AddPolygon(pts);
e.Graphics.FillPath(Brushes.LightGreen, graphicsPath);
e.Graphics.DrawPath(Pens.DarkGreen, graphicsPath);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句