October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Graphics Programming Using C# - Part 2

  • July 31, 2002
  • By Anand Narayanaswamy
  • Send Email »
  • More Articles »

Filling the Shapes with Patterns and Colors

The Brush class of the System.Drawing.Drawing2D namespace provides functionalities for creating shapes by filling the inside of them with colors and patterns. By making use of various Brush classes such as Solid Brush, Hatch Style Brush, Gradient Brush and Textured Brush, you can modify the appearance of filled shapes.

For instance, a rectangle can be filled with vertical and horizontal lines by using its FillXXX() methods. Like this, you have to apply the relevant methods for creating various shapes.

Working with Brush Objects

As already examined, the brush class is used to fill the shapes with a given color, pattern, and image. Listing 2 examines the usage of the Solid Brush by applying its object in FillRectangle() method.

Listing 2

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

public class Solidbru:Form  {

  public Solidbru()  {
   this.Text = "Using Solid Brushes";
   this.Paint += new PaintEventHandler(Fill_Graph);
  }

  public void Fill_Graph(object sender,PaintEventArgs e)
  {
   Graphics g = e.Graphics;

   //Creates a SolidBrush and fills the rectangle

   SolidBrush sb = new SolidBrush(Color.Pink);
   g.FillRectangle(sb,50,50,150,150);
  }

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

}

The preceding code creates a Solid Brush object named sb:

SolidBrush sb = new SolidBrush(Color.Pink);

This object is then applied to the FillRectangle() method of the Graphics class, which ultimately fills the rectangle with a Pink color:

g.FillRectangle(sb,50,50,150,150);

Having seen the working of the Solid Brush, let's see the usage of another Brush class called Hatch Brush, which fills the shapes with patterns such as DiagonalCross, horizontal and vertical lines, and so forth. Listing 3 examines the usage of the Brush class:

Listing 3

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

public class Hatchbru:Form  {

  public Hatchbru()  {
   this.Text = "Using Solid Brushes";
   this.Paint += new PaintEventHandler(Fill_Graph);
  }

  public void Fill_Graph(object sender,PaintEventArgs e)
  {
   Graphics g = e.Graphics;

   //Creates a Hatch Style,Brush and fills the rectangle
   /*Various HatchStyle values are
     DiagonalCross,ForwardDiagonal,
     Horizontal, Vertical, Solid etc. */

   HatchStyle hs = HatchStyle.Cross;
   HatchBrush sb = new 
   HatchBrush(hs,Color.Blue,Color.Red);
   g.FillRectangle(sb,50,50,150,150);
  }

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

From Listing 3, you can see that the first of the values of the HatchStyle enumerator is applied by using the HatchStyle class. Then this object is called to create an object of Hatch Brush class. Finally, this object is applied to the FillRectangle() method as shown above. The output is shown in Figure 2.

Figure 2  Output





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel