How to: Draw Outlined Shapes
This example demonstrates how to draw outlined ellipses and rectangles on a form.
Example
private void DrawEllipse()
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawEllipse(myPen, new Rectangle(0,0,200,300));
myPen.Dispose();
formGraphics.Dispose();
}
private void DrawRectangle()
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawRectangle(myPen, new Rectangle(0,0,200,300));
myPen.Dispose();
formGraphics.Dispose();
}
Compiling the Code
This example requires:
- A Windows Forms Application project with a form named formGraphics.
The code must be within the scope of the Form class. The instance of the form is represented by this.
Robust Programming
You should always call Dispose on any objects that consume system resources, such as Brush and Graphics objects.
See Also
Concepts
Designing a User Interface in Visual C#