Graphics.Clear() Method in C# with Examples
Graphics.Clear(color) Method is used to clear the canvas and paints it up with the specified background color.
Syntax: public void Clear (System.Drawing.Color color);
Parameter:
color: Color identifier which contains RGB values, to colour the background of canvas.
Example 1:
// C# snippet to print GeeksForGeeksusing System;using System.Drawing;using System.Drawing.Printing;using System.Windows.Forms;namespace GFG { class PrintableForm : Form { // Main Method public static void Main() { Application.Run(new PrintableForm()); } public PrintableForm() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs e) { Font font = new Font("Arial", 16); SolidBrush brush = new SolidBrush(Color.Black); PointF point = new PointF(10.0F, 10.0F); e.Graphics.DrawString("GeeksForGeeks", font, brush, point); }}} |
Output:

Example 2:
// C# program to illustrate the Clear Method// C# snippet to print GeeksForGeeksusing System;using System.Drawing;using System.Drawing.Printing;using System.Windows.Forms;namespace GFG { class PrintableForm : Form { // Main Method public static void Main() { Application.Run(new PrintableForm()); } public PrintableForm() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs e) { e.Graphics.Clear(Color.Coral); }}} |
Output:

Reference:


