Search code examples
c#drawdrawellipse

Change the height level contained in the ellipse


I draw the ellipse with the code as shown below. How can I make the height of the red color is inside the ellipse can be changed for example from 0% -100%. If 0% the meanings the level of red height is empty. If 50% the meanings height level of the red color is half of the ellipse. If 100% the meanings height level of the red color is full. Thank You.

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Rectangle r1= new Rectangle(10, 130, 60, 60);

        // Create solid brush.
        SolidBrush redBrush = new SolidBrush(Color.Red);

        // Create location and size of ellipse.
        float x = 20F;
        float y = 20F;
        float width = 80.0F;
        float height = 200.0F;

        // Fill ellipse on screen.
        e.Graphics.FillEllipse(redBrush, x, y, width, height);
    }

Solution

  • Please, try the following code:

    void panel1_Paint(object sender, PaintEventArgs e)
        float percent = 0.75f;
        RectangleF bounds = new RectangleF(20, 20, 80, 200);
        FillEllipse(e.Graphics, bounds, percent);
    }
    static void FillEllipse(Graphics g, RectangleF bounds, float percent) {
        g.DrawEllipse(Pens.Red, bounds);
        g.SetClip(new RectangleF(
            bounds.X,
            bounds.Y + (1f - percent) * bounds.Height,
            bounds.Width,
            percent * bounds.Height));
    
        g.FillEllipse(Brushes.Red, bounds);
        g.ResetClip();
    }