This example shows that how to draw ellipse or rectangle on the windows form This example draws a circle and a square on a form.
Example
following example requires windows form and create its Paint event named ‘Form1_Paint’.
Code : Draws an ellipse
First we create graphics for the Form with the help of Me.CreateGraphics.and then we define a bounded rectrangle specified by the pair of coordinates,a height, and a width.
DrawEllipse Draws an ellipse specified by a bounding Rectangle structure.
[VB.Net]
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim grp As System.Drawing.Graphics = Me.CreateGraphics() Dim rect As New System.Drawing.Rectangle(150, 150, 250, 250) grp.DrawEllipse(System.Drawing.Pens.Black, rect) End Sub
[C#]
private void Form1_Paint(object sender, PaintEventArgs e) { System.Drawing.Graphics grp = this.CreateGraphics(); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(50, 50, 250, 250); grp.DrawEllipse(System.Drawing.Pens.Black, rect); }
Code: Fill Ellipse
[VB.net]
Private Sub Form1_Paint(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim grp As System.Drawing.Graphics = Me.CreateGraphics() Dim rect As New System.Drawing.Rectangle(50, 50, 250, 250) Dim redBrush As New SolidBrush(Color.Red) grp.FillEllipse(redBrush, rect) End Sub
[C#]
private void Form1_Paint(object sender, PaintEventArgs e) { System.Drawing.Graphics grp = this.CreateGraphics(); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(50, 50, 250, 250); SolidBrush redBrush = new SolidBrush(Color.Red); grp.FillEllipse(redBrush, rect); }