修改后的 GDI 弧

GDI arc with modification

我对 GDI 有疑问。我在 WinForms 中完成。这就是我得到的:

还有我的代码:

Graphics phantom = this.pictureBox1.CreateGraphics();
Pen blackPen = new Pen(Color.Black, 3);
Rectangle rect = new Rectangle(0, 0, 200, 150);
float startAngle = 180F;
float sweepAngle = 180F;
phantom.DrawArc(blackPen, rect, startAngle, sweepAngle);
phantom.Dispose();

我想要这样的东西:

真对不起我的画功。是否可以从弧本身创建这样的东西,还是我必须从椭圆中创建这样的东西?我不知道该怎么做。欢迎任何提示。谢谢

来自我对原文的评论post:

You have two circles, let's call them lower and upper. Define the upper circle as a GraphicsPath and pass that to the constructor of a Region. Now pass that Region to e.Graphics via the ExcludeClip method. Now draw the lower circle, which will be missing the top part because of the clipping. Next, Reset() the Graphics and define the lower circle in a GraphicsPath. Use Graphics.Clip() this time, and chase that with drawing the upper circle. It will only be visible where the lower circle clip was.

概念验证:

代码:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics phantom = e.Graphics;
    using (Pen blackPen = new Pen(Color.Black, 3))
    {
        Rectangle upper = new Rectangle(-50, -250, 300, 300);
        GraphicsPath upperGP = new GraphicsPath();
        upperGP.AddEllipse(upper);
        using (Region upperRgn = new Region(upperGP))
        {
            Rectangle lower = new Rectangle(0, 0, 200, 150);
            GraphicsPath lowerGP = new GraphicsPath();
            lowerGP.AddEllipse(lower);

            float startAngle = 180F;
            float sweepAngle = 180F;

            phantom.ExcludeClip(upperRgn);
            phantom.DrawArc(blackPen, lower, startAngle, sweepAngle);

            phantom.ResetClip();
            phantom.SetClip(lowerGP);
            phantom.DrawEllipse(blackPen, upper);
        }


    }
}