Drawing a dotted circle using C#.NET windows form

Dotted circle can be used for showing circular progress. Here is the code to draw a dotted/dashed circle using C# in windows form application:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int angle = 0;
        int Startangle = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
                       
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            //draw a dashed cricle
            using(Graphics g = panel1.CreateGraphics())
            {
                System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(5, 5, 200, 200);
                g.SmoothingMode = SmoothingMode.AntiAlias;
                var dottedPen = new Pen(new SolidBrush(Color.FromArgb(255, 51, 51)), 10) { DashPattern = new[] { 0.5f, 0.5f } };
                g.DrawArc(dottedPen, rectangle, Startangle, angle);                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new Thread(() =>
                {
                    for (int i = 1; i <= 360; i++)
                    {
                        Startangle = 0;
                        angle++;                        
                        Thread.Sleep(10);
                        panel1.Invalidate();
                    }
                }).Start();
        }
    }
}



Comments