如何在C#Winform中用颜色填充弯曲区域?

时间:2015-12-11 10:31:20

标签: c# winforms

我想在矩形中绘制带颜色的曲面区域(在我的情况下为黑色)。

我在OnPaint事件中尝试了多个FillPie,FillEllipse,但是无法做到,我想这样绘制Fill Curved Area

我尝试过以下代码。但这不是我想要的

protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics gr = e.Graphics;
        int x = 50;
        int y = 50;
        int width = 100;
        int height = 100;
    Rectangle rect = new Rectangle(x, y, width / 2, height / 2);
    gr.FillRectangle(Brushes.Black, rect);
    gr.FillPie(Brushes.White, x, y, width, height, 180, 90);

    using (Pen pen = new Pen(Color.Yellow, 1))
        gr.DrawArc(pen, x, y, width, height, 180, 90);
}

这段代码就是这样画的。我不想创建额外的矩形。 MyCode

1 个答案:

答案 0 :(得分:0)

我不能100%确定这是否是你想要的:

enter image description here

我通过使用指定矩形的四分之一创建Region来实现此目的。然后我使用GraphicsPath从中排除馅饼。然后使用带有黑色笔刷的Graphics.FillRegion填充生成的曲线:

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics gr = e.Graphics;
        int x = 50;
        int y = 50;
        int width = 100;
        int height = 100;

        Rectangle rect = new Rectangle(x, y, width/ 2, height / 2);
        Region r = new Region(rect);

        GraphicsPath path = new GraphicsPath();
        path.AddPie(x, y, width, height, 180, 90);
        r.Exclude(path);
        gr.FillRegion(Brushes.Black,r);
    }