Page 1 of 1

How to create Round Forms in C#

Posted: Wed Dec 23, 2009 2:24 am
by Neo
  1. Create Simple Window Application (C#).
  2. Set below properties in the form.

    startposition : CenterScreen
    MaximizeBox :False
    MinimizeBox :False
    Size :
    Width :400
    Height:400
    FormBorderStyle :none
  3. Put below code to form paint event

    Code: Select all

    private void Form1_Paint(object sender, PaintEventArgs e){
           System.Drawing.Drawing2D.GraphicsPath objGP = new System.Drawing.Drawing2D.GraphicsPath();
           objGP.AddEllipse(new Rectangle(0, 0,this.Width, this.Height));
           this.Region = new Region(objGP);
           this.FormBorderStyle = FormBorderStyle.None;
    }

Re: How to create Round Forms in C#

Posted: Wed Dec 23, 2009 3:42 pm
by Neofriend
Hey Friend, thank you for the help.
Am from DP, referring to this thread http://forums.digitalpoint.com/showthre ... st13202996

Can you please let me know how to have curved borders? More like css rounded boxes. More like the rounded css boxes you have here while writing this reply.

Thanks.

Re: How to create Round Forms in C#

Posted: Wed Dec 23, 2009 5:26 pm
by Neo
To get something like that with C#, you will have to create an image with rounded corners and then use it.

If you want that you can try the following code.

Code: Select all

using System.Drawing;
using System.Drawing.Drawing2D;

public Image RoundCorners(Image StartImage, int CornerRadius, Color BackgroundColor)
{
    CornerRadius *= 2;
    Bitmap RoundedImage = new Bitmap(StartImage.Width, StartImage.Height);
    Graphics g = Graphics.FromImage(RoundedImage);
    g.Clear(BackgroundColor);
    g.SmoothingMode = SmoothingMode.AntiAlias;
    Brush brush = new TextureBrush(StartImage);
    GraphicsPath gp = new GraphicsPath();
    gp.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90);
    gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90);
    gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
    gp.AddArc(0, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
    g.FillPath(brush, gp);
    return RoundedImage;
}

Image StartImage = Image.FromFile("YourImageFile.jpg");
Image RoundedImage = this.RoundCorners(StartImage, 25, Color.White);
//Use RoundedImage...
You may also try Graphics.SetClip() method as below.

Code: Select all

    public static Image OvalImage(Image img) {
        Bitmap bmp = new Bitmap(img.Width, img.Height);
        using (GraphicsPath gp = new GraphicsPath()) {
            gp.AddEllipse(0, 0, img.Width, img.Height);
            using (Graphics gr = Graphics.FromImage(bmp)) {
                gr.SetClip(gp);
                gr.DrawImage(img, Point.Empty);
            }
        }
        return bmp;
    }

Re: How to create Round Forms in C#

Posted: Wed Dec 23, 2009 5:29 pm
by Neofriend
Thank you, I'll look in to this soon.
Checking out how net send works with C# right now and some other useful trick such as tray icon when software is minimized.

Am new with C# and .NET so can you suggest me what could be such exciting features to use in my app?

Re: How to create Round Forms in C#

Posted: Wed Dec 23, 2009 5:33 pm
by Neo
Please start a new thread for that so it will be helpful for other users. I'll try to give you my fullest assistance.

Re: How to create Round Forms in C#

Posted: Wed Dec 23, 2009 5:34 pm
by Neofriend
Sure, i will do it. Thanks buddy.