How to: Draw a Filled Rectangle on a Form
This example demonstrates how to draw a filled rectangle on a form.
Example
System.Drawing.SolidBrush brush1 =
new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(brush1, new System.Drawing.Rectangle(0,0,200,300));
brush1.Dispose();
formGraphics.Dispose();
Compiling the Code
This example requires:
- A Windows Forms Application project with a form named formGraphics.
The code must be in 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#