Visual C#

GDI+ Shapes: Ellipses and Circles

An ellipse is a closed continuous line whose points are positioned so that two points exactly opposite each other have the exact same distant from a…

2 min read491 words2 code samples

Introduction#

An ellipse is defined as a closed, continuous loop where every point is positioned such that any two points on opposite sides are equidistant from a central focal point. It is generally visualized like this:

Because an ellipse fits inside a rectangle, in GDI+ programming an ellipse is defined in relation to the rectangle it would fit into. To draw an ellipse, you can use the Graphics.DrawEllipse() method, which comes in four versions whose syntaxes are:

C#
public void DrawEllipse(Pen pen,
		     Rectangle rect);
public void DrawEllipse(Pen pen,
		     RectangleF rect);
public void DrawEllipse(Pen pen,
		     int x,
		     int y,
		     int width,
		     int height);
public void DrawEllipse(Pen pen,
		     float x,
		     float y,
		     float width,
		     float height);

The arguments of this method play the same roll as those of the Graphics.DrawRectangle() method:

The parameters for this method serve the same roll as those used in the Graphics.DrawRectangle() method:

C#
private void Exercise_Paint(object sender, PaintEventArgs e)
{
        Pen penCurrent = new Pen(Color.Red);

        e.Graphics.DrawEllipse(penCurrent, new Rectangle(20, 20, 226, 144));
}

Project: Constructing a Circle

  1. Start a new Windows Application named RotatingCircles1
  2. Double-click middle of the form and change the file as follows: 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; namespace RotatingCircles1 { public partial class Form1 : Form { Graphics graphDrawingArea; Bitmap bmpDrawingArea; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { bmpDrawingArea = new Bitmap(Width, Height); graphDrawingArea = Graphics.FromImage(bmpDrawingArea); } } }
  3. From the Components section of the Toolbox, click Timer and click the form
  4. In the Properties window, change the following values: Enabled: True Interval: 20
  5. Under the form, double-click the timer1 object and make the following changes: 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; namespace RotatingCircles1 { public partial class Form1 : Form { Graphics graphDrawingArea; Bitmap bmpDrawingArea; static int mainRadius = 10; static int smallRadius = 5; static bool isMax; static bool smallRadiusMax; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { bmpDrawingArea = new Bitmap(Width, Height); graphDrawingArea = Graphics.FromImage(bmpDrawingArea); } void DrawCentralCircle(int CenterX, int CenterY, int Radius) { int start = CenterX - Radius; int end = CenterY - Radius; int diam = Radius * 2; graphDrawingArea.DrawEllipse(new Pen(Color.Blue), start, end, diam, diam); } void DrawCornerCircle(int CenterX, int CenterY, int Radius) { int start = CenterX - Radius; int end = CenterY - Radius; int diam = Radius * 2; graphDrawingArea.DrawEllipse(new Pen(Color.Red), start, end, diam, diam); } private void timer1_Tick(object sender, EventArgs e) { Graphics graph = Graphics.FromHwnd(this.Handle); int centerX = ClientRectangle.Width / 2; int centerY = ClientRectangle.Height / 2; if (isMax == true) mainRadius--; else mainRadius++; if (mainRadius > (ClientRectangle.Height / 2)) isMax = true; if (mainRadius < 10) isMax = false; if (smallRadiusMax == true) smallRadius--; else smallRadius++; if (smallRadius > 240) smallRadiusMax = true; if (smallRadius < 5) smallRadiusMax = false; // Central DrawCentralCircle(centerX, centerY, mainRadius); // Top-Left DrawCornerCircle(centerX / 2, centerY / 2, smallRadius); // Top-Right DrawCornerCircle(centerX + (centerX / 2), centerY / 2, smallRadius); // Bottom-Left DrawCornerCircle(centerX / 2, centerY + (centerY / 2), smallRadius); // BottomRight DrawCornerCircle(centerX + (centerX / 2), centerY + (centerY / 2), smallRadius); graph.DrawImage(bmpDrawingArea, 0, 0); } } }
  6. Execute the application to see the result
  7. Close the form

Home Copyright © 2010-2011 C# Key