Pens in GDI+

Consider the following code:

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

this.BackColor = Color.White ;
Graphics g = e.Graphics;

Pen p = new Pen ( Color.FromArgb ( 0 ,255, 0 ) ,3) ;
g.DrawRectangle ( p, 20, 20, 150, 100 ) ;

p.DashStyle = DashStyle.Dash ;
g.DrawRectangle ( p, 20, 150, 150, 100 ) ;

HatchBrush hb = new HatchBrush ( HatchStyle.BackwardDiagonal,
Color.Red, Color.Black ) ;
p.Brush = hb ;
g.DrawRectangle ( p, 200, 20, 150, 100 ) ;

p.SetLineCap ( LineCap.ArrowAnchor, LineCap.ArrowAnchor,
DashCap.Triangle ) ;
g.DrawRectangle ( p, 200, 150, 150 ,100 ) ;
g.DrawRectangle ( p, 200, 150, 150 ,100 ) ;

}

We can change the Background color of the Form by setting the BackColor property to some other color. We have created a Red Pen with Width 3.

We can change the style of Pen by setting its DashStyle property. The DashStyle Enumeration specifies the style of dashed lines drawn with a Pen. Here we have simply used the Dash member.

We can change the brush that determines the attributes of the Pen by setting the Brush property of the Pen to HatchBrush.

The SetLineCap( ) sets the values that determine the style of cap used to end lines drawn by the Pen. The first two parameters passed represent the startCap and endCap that represent the cap style to use at the beginning and ending of lines drawn by the Pen. The last parameter represents the cap style to use at the beginning or end of dashed lines drawn with this Pen. So we have used only this parameter. LineCap is an enumeration specifying the cap style for the Pen. The output of this program is:

Now suppose we want to fill the entire Form with an image, our code would simply be

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

Graphics g = e.Graphics ;
Size sz = this.ClientSize ;
Image img = Image.FromFile ( @"C:\fcode.jpg") ;
TextureBrush t = new TextureBrush ( img ) ;
for ( int h = 0 ; h <= sz.Height ;h += img.Height )
for ( int w = 0 ; w <= sz.Width ; w += img.Width )
g.FillRectangle( t, w, h, sz.Width, sz.Height ) ;

}

The ClientSize property gets or sets the size of the client area of the form. The Size structure represents the size of a rectangular region with an ordered pair of width and height. Rest is very obvious.

Posted bySumedh at 9:30 PM  

0 comments:

Post a Comment