Skip to content
Related Articles

Related Articles

Graphics.Clear() Method in C# with Examples
  • Last Updated : 04 Apr, 2019

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 GeeksForGeeks
using 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 GeeksForGeeks
using 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:

My Personal Notes arrow_drop_up
Recommended Articles
Page :