draw something onto the drawing surface (with the clipping region applied)

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    Rectangle rect = new Rectangle(10, 10, 80, 50);
    g.DrawRectangle(Pens.Black, rect);
    g.SetClip(rect);
    g.FillEllipse(Brushes.Blue, 20, 20, 100, 100);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


Hexagon Gradient Brush

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class HexagonGradientBrush: Form
{
const float fSide = 50; // Side (also radius) of hexagon

public static void Main()
{
Application.Run(new HexagonGradientBrush());
}
public HexagonGradientBrush()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
float fHalf = fSide * (float) Math.Sin(Math.PI / 3);

PointF[] aptf = {new PointF( fSide, 0),
new PointF( fSide * 1.5f, 0),
new PointF( fSide, 0),
new PointF( fSide / 2, -fHalf),
new PointF(-fSide / 2, -fHalf),
new PointF(-fSide, 0),
new PointF(-fSide * 1.5f, 0),
new PointF(-fSide, 0),
new PointF(-fSide / 2, fHalf),
new PointF( fSide / 2, fHalf) };

PathGradientBrush pgbrush1 = new PathGradientBrush(aptf, WrapMode.Tile);

for (int i = 0; i < aptf.Length; i++) { aptf[i].X += fSide * 1.5f; aptf[i].Y += fHalf; } PathGradientBrush pgbrush2 = new PathGradientBrush(aptf, WrapMode.Tile); grfx.FillRectangle(pgbrush1, 0, 0, cx, cy); grfx.FillRectangle(pgbrush2, 0, 0, cx, cy); } } [/csharp]

Hatch Brush Styles

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Drawing2D; // LinearGradientBrush

namespace HatchBrushStyles
{
public class HatchBrushStyles : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public HatchBrushStyles()
{
InitializeComponent();
this.Size = new Size(500, 150);
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = “HatchBrushStyles”;
}
#endregion

[STAThread]
static void Main()
{
Application.Run(new HatchBrushStyles());
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Font f = new Font(new FontFamily(“Times New Roman”), 10);
Brush fb = new SolidBrush(Color.Black);
Color cb = Color.Red, cf =Color.White;

int wi = 30, hi = 25, rectNb = 14;
int x, y;
HatchBrush hb = null;
for(int i = 0; i < 53; i++) { x = (int)(i % rectNb); y = (int)(i / rectNb); hb = new HatchBrush((HatchStyle)i, cf, cb); g.FillRectangle(hb, 2 + x*(5 + wi), 2 + y*(5 + hi), wi, hi); } fb.Dispose(); hb.Dispose(); g.Dispose(); } } } [/csharp]

Brush Style


   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GDI_Basics
{
    public class HatchBrushes : System.Windows.Forms.Form
    {
        private System.ComponentModel.Container components = null;

        public HatchBrushes()
        {
            InitializeComponent();
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(564, 390);
            this.Resize += new System.EventHandler(this.HatchBrushes_Resize);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.HatchBrushes_Paint);

        }
        #endregion

        private void HatchBrushes_Resize(object sender, System.EventArgs e)
        {
            this.Invalidate();
        }

        private void HatchBrushes_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            HatchBrush myBrush;
            int y = 20;
            int x = 20;

            foreach (HatchStyle brushStyle in System.Enum.GetValues(typeof(HatchStyle)))
            {
                myBrush = new HatchBrush(brushStyle, Color.Blue, Color.LightYellow);

                e.Graphics.FillRectangle(myBrush, x, y, 40, 20);

                e.Graphics.DrawString(brushStyle.ToString(), new Font("Tahoma", 8), 
                    Brushes.Black, 50 + x, y + 5);

                y += 30;
                if ((y + 30) > this.ClientSize.Height)
                {
                    y = 20;
                    x += 180;
                }
            }

        }

        [STAThread]
        static void Main() 
        {
            Application.Run(new HatchBrushes());
        }
    }
}


           
          


illustrates filling shapes with a brush


   

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Example21_4 : System.Windows.Forms.Form
{
  private System.ComponentModel.Container components = null;

  public Example21_4()
  {
    InitializeComponent();
  }

  private void InitializeComponent()
  {
    this.BackColor = System.Drawing.Color.White;
    this.ClientSize = new System.Drawing.Size(400, 400);
    this.Name = "Example21_4";
    this.Text = "Example21_4";
    this.Paint += new System.Windows.Forms.
      PaintEventHandler(this.Example21_4_Paint);
  }


  static void Main() 
  {
    Application.Run(new Example21_4());
  }

  private void Example21_4_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    Brush brSolid = new SolidBrush(Color.Blue);
    Brush brHatch = new HatchBrush(HatchStyle.HorizontalBrick,
      Color.Red, Color.Yellow);
    Brush brGradient = new LinearGradientBrush(new Rectangle(0, 0, 200, 200), Color.Black, Color.LightGray, 45, false);
    g.FillRectangle(brGradient, 10, 10, 200, 200);
    g.FillEllipse(brHatch, 200, 200, 150, 190);
    g.FillPie(brSolid, 0, 0, 300, 300, 285, 75);
  }
}


           
          


Gradient brush demo


   

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form{
    private System.ComponentModel.IContainer components;

  public Form1(){
    InitializeComponent();
  }


  #region Windows Form Designer generated code
  private void InitializeComponent(){
      this.components = new System.ComponentModel.Container();
      this.timer1 = new System.Windows.Forms.Timer(this.components);

      this.timer1.Enabled = true;
      this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.ClientSize = new System.Drawing.Size(292, 260);
      this.Name = "Form1";
      this.Text = "LinearGradientBrush Demo";
      this.Load += new System.EventHandler(this.Form1_Load);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
  #endregion

  static void Main(){
    Application.Run(new Form1());
  }

    private void Form1_Load(object sender, System.EventArgs e) {
      this.BackColor = Color.FromArgb(255, 0, 0, 255);  
    }

    private System.Windows.Forms.Timer timer1;

    private float angle = 0;

    private LinearGradientBrush GetBrush()
    {
      return new LinearGradientBrush(
        new Rectangle( 20, 20, 200, 100),
        Color.Orange,
        Color.Yellow,
        0.0F,
        true);
    }


    private void Rotate( Graphics graphics, LinearGradientBrush brush )
    {
      brush.RotateTransform(angle);
      brush.SetBlendTriangularShape(.5F);
      graphics.FillRectangle(brush, brush.Rectangle);
    }

    private void Rotate(Graphics graphics)
    {
      angle += 5 % 360;
      Rotate(graphics, GetBrush());
    }

    private void timer1_Tick(object sender, System.EventArgs e)
    {
      Rotate(CreateGraphics());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Rotate(e.Graphics);
    }
}



           
          


Use Brush to draw a Rectangle


   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

public class Form1 : System.Windows.Forms.Form{
  private System.ComponentModel.Container components = null;

  public Form1(){
    InitializeComponent();
        SetStyle(ControlStyles.Opaque, true);
  }
  protected override void Dispose( bool disposing ){
    if( disposing ){
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
    protected override void OnPaint(PaintEventArgs e) {
         Graphics g = e.Graphics;
         g.FillRectangle(Brushes.White, ClientRectangle);
         g.FillRectangle(Brushes.Red, new Rectangle(10, 10, 50, 50));
    }
  private void InitializeComponent(){
    this.components = new System.ComponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  static void Main() {
    Application.Run(new Form1());
  }
}