lilymz
lilymz

Reputation: 387

How to draw an ellipse C# via GDI?

I am new in C# and i am trying to draw a color filled ellipse, I foud some code but i couldn't figure out how to do it.

I tried with this code:

Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Aquamarine, 2);
g.DrawEllipse(pen, 10, 10, 100, 20);

But the method does not exist.

Colud you help me?

Thanks in advance.

Upvotes: 3

Views: 10240

Answers (1)

Mark Hall
Mark Hall

Reputation: 54532

You should do your graphics drawing in the forms Paint Event, otherwise as soon as the screen updates you will loose your drawing. This is a quick and dirty example on how to do so.

public Form1()
{
    InitializeComponent();
    this.Paint += new PaintEventHandler(Form1_Paint);
}

void Form1_Paint(object sender, PaintEventArgs e)
{
    Pen pen = new Pen(Color.Aquamarine,2);
    SolidBrush brush = new SolidBrush(Color.Aquamarine);

    e.Graphics.DrawEllipse(pen, 10, 10, 100, 20);
    e.Graphics.FillEllipse(brush, 10, 50, 100, 20);
}

Upvotes: 2

Related Questions