Working with Brushes

We have used four brushes SolidBrush, HatchBrush, TextureBrush, and LinearGradientBrush in the following program. Here is how the Paint event will be.

protected void Form1_Paint (object sender, System.WinForms.PaintEventArgs e)
{

Graphics g = e.Graphics;
Pen p = new Pen ( Color.Black, 4 ) ;

SolidBrush mysb = new SolidBrush(Color.FromArgb ( 0 ,255, 0 ));
g.FillRectangle ( mysb, 20, 20, 100, 200 ) ;
g.DrawRectangle ( p, 20, 20, 100, 200 ) ;

HatchBrush myhb = new HatchBrush (
HatchStyle.DiagonalCross,Color.Black,Color.Red );
g.FillRectangle ( myhb, 160, 20, 100, 200 ) ;
g.DrawRectangle ( p, 160, 20, 100, 200 ) ;

Image myimg = Image.FromFile( @"C:\fcode.jpg") ;
TextureBrush mytb = new TextureBrush ( myimg ) ;
g.FillRectangle ( mytb, 300, 20, 100, 200 ) ;
g.DrawRectangle ( p, 300, 20, 100, 200 ) ;

LinearGradientBrush mylgb = new LinearGradientBrush ( new
Point(440,20), new Point(540, 220),Color.Yellow,Color.Brown );
g.FillRectangle ( mylgb, 440, 20, 100, 200 ) ;
g.DrawRectangle ( p, 440, 20, 100, 200 ) ;

}

For using these brushes we need to add the Following code

using System.Drawing.Drawing2D ;

These brushes are defined in this namespace. Method for SolidBrush is the same.

For the HatchBrush we have specified the HatchStyle along with the Color. HatchStyle is an enum specifying different patterns available for HatchBrush objects

In the constructor of the TextureBrush class we need to pass an image. The Image object is created with the help of the FromFile( ) method. Using such a TextureBrush we can fill the rectangle.

To the constructor of a LinearGradientBrush we have passed two Points and two Colors. The points specify the starting and ending point of the Gradient, while the Colors represent the starting and ending Colors of the Gradient. The output looks like this:

Posted bySumedh at 9:30 PM  

0 comments:

Post a Comment