Search code examples
c#pictureboxedges

Rounded edges in picturebox C#


How to round edges in picturebox control. I Want to get angles like ellipse have but i dont know how to do it. I Use C#. Thanks!


Solution

  • Yes, no problem, you can give a control an arbitrary shape with its Region property. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    
    class OvalPictureBox : PictureBox {
        public OvalPictureBox() {
            this.BackColor = Color.DarkGray;
        }
        protected override void OnResize(EventArgs e) {
            base.OnResize(e);
            using (var gp = new GraphicsPath()) {
                gp.AddEllipse(new Rectangle(0, 0, this.Width-1, this.Height-1));
                this.Region = new Region(gp);
            }
        }
    }