Drawing shapes with GDI+

The class that encapsulates a GDI+ drawing surface is the Graphics class. It is in the System.Drawing namespace. We write all the drawing code in the Form1_Paint( ) method. Lets look at the simplest example used to display various shapes in our form.

The Form1_Paint( ) event handler is called when a control is repainted. The PaintEventArgs class specifies the Graphics to use to paint the control. In this Paint event we plan to draw some shapes with the help of an object of the Graphics class. We have created a Graphics object g and initialized it to the Graphics property of the PaintEventArgs class.

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

Graphics g = e.Graphics ;

Pen mypen = new Pen( Color.Red, 5 ) ;
SolidBrush mybrush = new SolidBrush ( Color.Blue ) ;

g.DrawLine( mypen, 20, 30, 200, 60 ) ;

g.FillRectangle( mybrush, 20, 70, 150, 100 );
g.DrawRectangle( mypen, 20, 70, 150, 100 );

g.FillEllipse( mybrush, 20, 200, 150, 100);
g.DrawEllipse( mypen, 20, 200, 150, 100);

mybrush.Color = Color.FromArgb(0,255,0) ;

g.FillPie ( mybrush, 210, 40, 100, 100, 0, 140 ) ;
g.DrawPie ( mypen , 210, 40, 100, 100, 90, 140 ) ;
mybrush.Color = Color.Yellow ;

Point[] p = {new Point(365,270), new Point(300,265),
new Point( 240,255),new Point( 245,200),new Point( 340,190) } ;
g.FillPolygon (mybrush, p ) ;
g.DrawPolygon( mypen , p ) ;

}


First we have created a Pen with the specified Color and Width and a SolidBrush with the specified Color. Color is a structure. In C# the Brush class is an abstract class from which different kinds of brushes are derived. The SolidBrush class is one of them. We will see the other brushes later.

The DrawLine( ) method draws a line with a specified Pen at the specified points. All the �Fill� methods draw the shapes with solid fill and hence a brush must be specified in such methods. All the �Draw� methods draw the outline of the shape and hence a pen must be passed in such methods.

The second and third parameters passed to the DrawRectangle( ) and the FillRectangle( ) methods specify the upper-left corner�s x and y coordinates from where the rectangle would be drawn. The last two parameters denote the width and height of the rectangle.

The second and third parameters passed to the DrawEllipse( ) and FillEllipse( ) methods specify the upper-left corner�s x and y coordinate from where the bounding rectangle that defines the ellipse is drawn. The last two parameters denote the width and height of the bounding rectangle.

The DrawPie( ) draws the outline of a pie section defined by an ellipse and two radial lines. The second, third, fourth and fifth parameters specify the x-coordinate, y-coordinate width and height of the bounding rectangle that defines the ellipse. The last two parameters denote the startAngle, an angle measured clockwise from the x-axis to the first side of the pie section and the sweepAngle, an angle measured clockwise from the x axis to the second side of the pie section. When we Compile and execute it , we get this:

There are two ways to change the Color of a Pen or Brush. Either equate it to a member of the struct Color, or use the FromArgb( ) method by passing 3 ints specifying the red, blue and green components of the desired Color.

We need to pass an array of type Point to the DrawPolygon( ) and FillPolygon( ) method, which define the points representing the Polygon.

Posted bySumedh at 9:29 PM  

0 comments:

Post a Comment