Pens, Lines, and Rectangles
To draw lines with GDI+ you need to create a Graphics object and a Pen object. The Graphics object provides the methods that actually do the drawing, and the Pen object stores attributes, such as line color, width, and style. To draw a line, call the DrawLine method of the Graphics object. The Pen object is passed as one of the arguments to the DrawLine method. The following example draws a line from the point (4, 2) to the point (12, 6):
myGraphics.DrawLine(myPen, 4, 2, 12, 6)
[C#]
myGraphics.DrawLine(myPen, 4, 2, 12, 6);
DrawLine is an overloaded method of the Graphics class, so there are several ways you can supply it with arguments. For example, you can construct two Point objects and pass the Point objects as arguments to the DrawLine method:
Dim myStartPoint As New Point(4, 2)
Dim myEndPoint As New Point(12, 6)
myGraphics.DrawLine(myPen, myStartPoint, myEndPoint)
[C#]
Point myStartPoint = new Point(4, 2);
Point myEndPoint = new Point(12, 6);
myGraphics.DrawLine(myPen, myStartPoint, myEndPoint);
You can specify certain attributes when you construct a Pen object. For example, one Pen constructor allows you to specify color and width. The following example draws a blue line of width 2 from (0, 0) to (60, 30):
Dim myPen As New Pen(Color.Blue, 2)
myGraphics.DrawLine(myPen, 0, 0, 60, 30)
[C#]
Pen myPen = new Pen(Color.Blue, 2);
myGraphics.DrawLine(myPen, 0, 0, 60, 30);
The Pen object also exposes properties, such as DashStyle, that you can use to specify features of the line. The following example draws a dashed line from (100, 50) to (300, 80):
myPen.DashStyle = DashStyle.Dash
myGraphics.DrawLine(myPen, 100, 50, 300, 80)
[C#]
myPen.DashStyle = DashStyle.Dash;
myGraphics.DrawLine(myPen, 100, 50, 300, 80);
You can use the properties of the Pen object to set many more attributes of the line. The StartCap and EndCap properties specify the appearance of the ends of the line; the ends can be flat, square, rounded, triangular, or a custom shape. The LineJoin property lets you specify whether connected lines are mitered (joined with sharp corners), beveled, rounded, or clipped. The following illustration shows lines with various cap and join styles.
Drawing rectangles with GDI+ is similar to drawing lines. To draw a rectangle, you need a Graphics object and a Pen object. The Graphics object provides a DrawRectangle method, and the Pen object stores attributes, such as line width and color. The Pen object is passed as one of the arguments to the DrawRectangle method. The following example draws a rectangle with its upper-left corner at (100, 50), a width of 80, and a height of 40:
myGraphics.DrawRectangle(myPen, 100, 50, 80, 40)
[C#]
myGraphics.DrawRectangle(myPen, 100, 50, 80, 40);
DrawRectangle is an overloaded method of the Graphics class, so there are several ways you can supply it with arguments. For example, you can construct a Rectangle object and pass the Rectangle object to the DrawRectangle method as an argument:
Dim myRectangle As New Rectangle(100, 50, 80, 40)
myGraphics.DrawRectangle(myPen, myRectangle)
[C#]
Rectangle myRectangle = new Rectangle(100, 50, 80, 40);
myGraphics.DrawRectangle(myPen, myRectangle);
A Rectangle object has methods and properties for manipulating and gathering information about the rectangle. For example, the Inflate and Offset methods change the size and position of the rectangle. The IntersectsWith method tells you whether the rectangle intersects another given rectangle, and the Contains method tells you whether a given point is inside the rectangle.