如何绘制圆弧填充的路径

时间:2019-07-09 06:17:47

标签: gdi+

可能是一个简单的问题:

如何绘制由直线和圆弧构建的填充图形路径?问题在于,GDI +仅以顺时针方向定义了从起始角度到终止角度的弧。

Image to be made

一个示例(任何语言)都很棒!

应通过将圆弧的终点设置为下一行的起点等来实现。

1 个答案:

答案 0 :(得分:0)

一种方法=>

  • 结果:

enter image description here

  • 代码(C#,VS 2015): (在屏幕DC上进行测试):
using (Graphics gr = Graphics.FromHwnd(IntPtr.Zero))
{
   RectangleF rect = new Rectangle(500, 100, 400, 400 / 2);
   RectangleF rectArc;
   float nXradius = 60;
   float nYradius = 60;

   PointF point1, point2;
   GraphicsPath gp = new GraphicsPath();

   // Upper Left
   rectArc = new RectangleF(rect.X, rect.Y, 2 * nXradius, 2 * nYradius);
   gp.AddArc(rectArc, 180, 90);
   point1 = new PointF(rect.X + nXradius, rect.Y);                   

   // Top Border                   
   point2 = new PointF(rect.Right - nXradius, rect.Y);                   
   gp.AddLine(point1, point2);

   // Upper Right.
   rectArc = new RectangleF(rect.Right - 2 * nXradius, rect.Y, 2 * nXradius, 2 * nYradius);
   gp.AddArc(rectArc, 270, 90);
   point1 = new PointF(rect.Right, rect.Y + nYradius);

   // Right Border
   point2 = new PointF(rect.Right, rect.Bottom - nYradius);                   
   gp.AddLine(point1, point2);

   // Lower Right
   rectArc = new RectangleF(rect.Right, rect.Bottom - 2 * nYradius, 2 * nXradius, 2 * nYradius);
   gp.AddArc(rectArc, -180, -90);
   point1 = new PointF(rect.Right + nXradius, rect.Bottom);

   // Bottom Border                  
   point2 = new PointF(rect.X - nXradius, rect.Bottom);                  
   gp.AddLine(point1, point2);

   // Lower Left
   rectArc = new RectangleF(rect.X - 2 * nXradius, rect.Bottom - 2 * nYradius, 2 * nXradius, 2 * nYradius);
   gp.AddArc(rectArc, 90, -90);
   point1 = new PointF(rect.X, rect.Bottom - nYradius);                  

   // Left Border
   point2 = new PointF(rect.X, rect.Y + nYradius);                   
   gp.AddLine(point1, point2);

   gp.CloseFigure();
   System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 10);
   pen.LineJoin = LineJoin.Round;
   gr.SmoothingMode = SmoothingMode.AntiAlias;
   gr.DrawPath(pen, gp);
   gr.FillPath(System.Drawing.Brushes.Orange, gp);
}