Graphics.Clear(Color) Method

Definition

Clears the entire drawing surface and fills it with the specified background color.

C#
public void Clear(System.Drawing.Color color);

Parameters

color
Color

The background color of the drawing surface.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code clears the drawing surface of the Graphics and sets the background color to the system-defined teal color.

C#
private void ClearColor(PaintEventArgs e)
{
    // Clear screen with teal background.
    e.Graphics.Clear(Color.Teal);
}

Remarks

Effectively, the Clear method fills an area with a solid color brush of the specified color. The transparency of the specified color is maintained.

This method clears the state of the graphics object and should not be called when the graphics object cannot be updated. For example, if the Clear method is called on a secure desktop in a terminal server session, an ExternalException may occur, leaving the Graphics object in an inconsistent state.

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10