📜  C#中的Graphics.Clear()方法与示例

📅  最后修改于: 2021-05-29 17:52:20             🧑  作者: Mango

Graphics.Clear(color)方法用于清除画布,并使用指定的背景颜色对其进行绘制。

范例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);
    }
}
}

输出:

范例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);
    }
}
}

输出:

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.graphics.clear?view=netframework-4.7.2