Go to C winform to clear all lines or graphics drawn by GDI

  • 2020-05-12 03:03:54
  • OfStack

In the C# winform application, you can draw outgoing lines or graphs with GDI.
1. Draw lines or graphics on the main form

      using (Graphics g = this.CreateGraphics())
      {
            g.DrawLine(Pens.Blue, new Point(10, 10), new Point(100, 100));
      }

2. Draw a line or graph on a specified container, such as panel1

     using (Graphics g = this.panel1.CreateGraphics())
     {
            g.DrawLine(Pens.Blue, new Point(10, 10), new Point(100, 100));
     }

But sometimes you want to clear all the lines or graphics drawn by GDI from the current form by using the following method.

      using (Graphics g = this.CreateGraphics())
      {
             g.Clear(this.BackColor);
      }

The g.Clear () function is used to redraw the background of the form with the specified color. The parameter in the Clear function is the color to be drawn. When the parameter is set to this.BackColor, which is the background color of the current form, all lines or graphics drawn by GDI can be cleared from the current form.

Related articles: