Code: Drawing a Filled Rectangle on a Form (Visual C#)
This example demonstrates how to draw a filled rectangle on a form.
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.FillRectangle(myBrush, new Rectangle(0,0,200,300)); myBrush.Dispose(); formGraphics.Dispose();
Compiling the Code
This example requires:
- A Windows Application project.
The code needs to be within the scope of the form class. The instance of the form is represented by this.
You should always call Dispose on any objects that consume system resources, such as Brush and Graphics objects.