Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i draw a circle in c# windows form( half colored and half not)
i know how draw circle but i cant color it
i hope you understand me
and this how i draw circle
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawEllipse(Pens.Red, 50, 50, 100, 100);
        }
    }
}

is that right or not
Posted
Comments
Mehdi Gholam 1-Nov-11 7:06am    
Have you tested it?

If you want it half 'coloured' (you mean filled in right?) and half not, it's not a circle. It's two arcs. Use Graphics.DrawArc[^] and FillPie.

(Edit: Whose idea was it to call the Fill and Draw items different things ...)
 
Share this answer
 
v2
Comments
rawa1990 1-Nov-11 8:36am    
thanks for the idea
Sergey Alexandrovich Kryukov 11-Dec-11 19:16pm    
Agree, my 5.
I added some detail and credited this correct answer in my solution.
--SA
Bob's answer is correct. See some more important details below:

All you need is System.Drawing.Graphics.FillPie(Brush, Rectangle) before of DrawEllipse, see http://msdn.microsoft.com/en-us/library/dw5wshbd.aspx[^].

If you want to allocate a brush, don't dispose it manually as UJimbo advised. That code is wrong because it will leak memory/resources in the case of exceptions. The right approach is using using statement:

C#
Rectangle ellipseBounds = new Rectangle(50, 50, 100, 100); //like in your code sample
using (Brush brush = new SolidBrush(/* some parameters */)) {
    e.Graphics.FillPie(brush, ellipseBounds, 0, 180); //for example, do it before drawing lines.
    e.Graphics.DrawEllipse(Color.Red, ellipseBounds);
} //brush.Dispose will be automatically called here, even if exception is thrown


Same thing about allocation of pens, etc.

—SA
 
Share this answer
 
v4
You can thank BobJanova for that, DrawArc was a good hint.

System.Drawing.SolidBrush brush0 = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
System.Drawing.SolidBrush brush1 = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.SolidBrush brush2 = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);

System.Drawing.Pen thePen = new System.Drawing.Pen(brush0);
System.Drawing.Graphics formGraphics = this.CreateGraphics();

formGraphics.DrawArc(thePen, new System.Drawing.Rectangle(150, 50, 100, 100), 0, 180);
formGraphics.DrawArc(thePen, new System.Drawing.Rectangle(150, 50, 100, 100), 180, 360);
formGraphics.FillPie(brush1, new System.Drawing.Rectangle(150, 50, 100, 100), 0, 180);
formGraphics.FillPie(brush2, new System.Drawing.Rectangle(150, 50, 100, 100), 180, 180);

brush0.Dispose();
brush1.Dispose();
brush2.Dispose();
thePen.Dispose();
formGraphics.Dispose();
 
Share this answer
 
Comments
rawa1990 1-Nov-11 8:36am    
thank you
UJimbo 1-Nov-11 8:38am    
You are welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900