-1

I am currently trying to draw a gradient filled rounded rectangle onto a panel bar in a form.

From some research I found some code that allowed me to create a custom rectangle :

static class CustomRectangle 
{
    public static GraphicsPath RoundedRect(Rectangle bounds, int radius)
    {
        int diameter = radius * 2;
        Size size = new Size(diameter, diameter);
        Rectangle arc = new Rectangle(bounds.Location, size);
        GraphicsPath path = new GraphicsPath();

        if (radius == 0)
        {
            path.AddRectangle(bounds);
            return path;
        }

        // top left arc  
        path.AddArc(arc, 180, 90);

        // top right arc  
        arc.X = bounds.Right - diameter;
        path.AddArc(arc, 270, 90);

        // bottom right arc  
        arc.Y = bounds.Bottom - diameter;
        path.AddArc(arc, 0, 90);

        // bottom left arc 
        arc.X = bounds.Left;
        path.AddArc(arc, 90, 90);

        path.CloseFigure();
        return path;
    }

    public static void FillRoundedRectangle(this Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius)
    {
        if (graphics == null)
            throw new ArgumentNullException("graphics");
        if (brush == null)
            throw new ArgumentNullException("brush");

        using (GraphicsPath path = RoundedRect(bounds, cornerRadius))
        {
            graphics.FillPath(brush, path);
        }
    }

Credit to this page

Next using this custom rectangle, I have tried using the paint method for the bar panel.

private void quickMenuBar_Paint(object sender, PaintEventArgs e)
    {
        LinearGradientBrush myBrush = new LinearGradientBrush(new Point(20, 20), new Point(120, 520), Color.DarkBlue, Color.RoyalBlue);
        System.Drawing.Graphics formGraphics = this.CreateGraphics();

        CustomRectangle.FillRoundedRectangle(formGraphics, myBrush, new System.Drawing.Rectangle(20, 20, 100, 500), 25);
        myBrush.Dispose();
        formGraphics.Dispose();
}

But after executing this code, it only prints a rounded rectangle directly onto the form and behind the bar panel.

I have other methods that fill a panel with a standard rectangle using PaintEventArgs e:

e.Graphics.FillRectangle(myBrush , otherBar.ClientRectangle);

So obviously I need to use PaintEventArgs e in my custom rectangle method, but I do not know how or where to.

If there are better ways than this way of drawing a rounded rectnagles, please share.

Kingler
  • 5
  • 6
  • _System.Drawing.Graphics formGraphics = this.CreateGraphics();_ Never use `control.CreateGraphics`! Never try to cache a `Graphics` object! Either draw into a `Bitmap bmp` using a `Graphics g = Graphics.FromImage(bmp)` or in the `Paint` event of a control, using the `e.Graphics` parameter.. – TaW Jul 21 '16 at 09:12
  • Try using Graphics for panel, not for the form. – Kamalesh Wankhede Jul 21 '16 at 09:12

1 Answers1

2

You generally should never be using CreateGraphics(). Simply remove this line:

System.Drawing.Graphics formGraphics = this.CreateGraphics();

And use e.Graphics where you would previously use formGraphics. The Paint event is basically asking you to "paint something for me, here's the graphics object to paint on".

Since you're already providing a Graphics object instance to your rounded rectangle method, no changes are required there.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72