0

I'm New in c sharp, i'm trying to make a circular button. the code below is making a ellipse for me but it is not looking smooth, also it is having partially hidden lines at right and bottom as shown in image ellipse button

 class ButtonEllipse: Button
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            GraphicsPath graphics = new GraphicsPath();
            Rectangle myEllipse = new Rectangle(0, 0, this.ClientSize.Width,this.ClientSize.Height);
            graphics.AddEllipse(myEllipse);            
            Pen myPen = new Pen(Color.Black, 2);
            this.Region = new System.Drawing.Region(graphics);   
            base.OnPaint(e);
         }
     }

can you please guide me how to get a exact and smooth circular button.

sanghmitra
  • 73
  • 7
  • No Regions, those are difficult to anti-alias. See the example here: [Translucent circle with text](https://stackoverflow.com/a/51435842/7444103). You'll have to draw all the parts yourself, if you want a decent result (including the Button's states). You can probably do something similar using Images that reproduce the states (Default, Entered, Hot, Pressed). – Jimi Jun 09 '20 at 09:17
  • Create and set the Region ONCE, or whenever the button changes size. There is no need to do this every time it repaints itself... – Idle_Mind Jun 09 '20 at 14:48
  • You're also creating and LEAKING a Pen. Create Pens/Brushes in a `using()` block so it correctly gets disposed of. – Idle_Mind Jun 09 '20 at 14:50

1 Answers1

0

Try setting graphics.SmoothingMode = SmoothingMode.AntiAlias; Read more about antialiazing.

You should perhaps also paint the button graphics directly rather than using the Region. See DrawEllipse and FillEllipse.

JonasH
  • 28,608
  • 2
  • 10
  • 23