How to create Round Forms in C#

.NET programming topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to create Round Forms in C#

Post by Neo » Wed Dec 23, 2009 2:24 am

  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;
    }
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to create Round Forms in C#

Post by Neofriend » Wed Dec 23, 2009 3:42 pm

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.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to create Round Forms in C#

Post by Neo » Wed Dec 23, 2009 5:26 pm

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;
    }
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to create Round Forms in C#

Post by Neofriend » Wed Dec 23, 2009 5:29 pm

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?
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to create Round Forms in C#

Post by Neo » Wed Dec 23, 2009 5:33 pm

Please start a new thread for that so it will be helpful for other users. I'll try to give you my fullest assistance.
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to create Round Forms in C#

Post by Neofriend » Wed Dec 23, 2009 5:34 pm

Sure, i will do it. Thanks buddy.
Post Reply

Return to “.NET Programming”