vivek jain
vivek jain

Reputation: 79

How to fill curved area with color in C# Winform?

I want to draw curved area with color(black in my case) in a rectangle.

I tried multiple things FillPie, FillEllipse in OnPaint event but not able to do, I want to draw like thisFill Curved Area

I have tried the below code. but this is not what I want

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);
}

this code is drawing like this. I dont want to create extra rectangle. MyCode

Upvotes: 1

Views: 746

Answers (1)

René Vogt
René Vogt

Reputation: 43876

I'm not 100% sure if this is that what you wanted:

enter image description here

I achieved this by creating a Region with a quarter of the specified rectangle. I then exclude the pie from it using a GraphicsPath. The resulting curve is then filled using Graphics.FillRegion with a black brush:

    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);
    }

Upvotes: 1

Related Questions