

Once a Rectangle object has been instantiated the syntax to call DrawRectangle() is as follows: Rectangle rectangleObj = new Rectangle ( x, y, width, height) The syntax for creating a Rectangle object in C# is as follows: The alternative is to pass through a Rectangle object in place of the co-ordinates and dimensions. Graphicsobj.DrawRectangle( pen, x, y, width, height) We will begin by looking at drawing a rectangle without a pre-created Rectangle object. One is to pass through a Rectangle object and Pen and the other is to create an instance of a Rectangle object and pass that through along with the Pen. There are two ways to use the DrawRectangle() method. Now that we have a Paint() event handler, a Graphics Object and Pen we can now begin to draw.įor the purposes of drawing rectangles and squares in C# the GraphicsObject provides the DrawRectangle() method. Properties such as the color and width may similarly be changed after a Pen has been created: myPen.DashStyle = .DashDotDot For example, the DashStyle property can be modified to change the style of line (i.e Dash, DashDot, DashDotDot, Dot, Solid or Custom).

Once a Pen object has been created other properties may be changed. Where variable_name is the name to be assigned to the Pen object, color is the color of the pen and width is the width of the lines to be drawn by the pen.įor example, we can create red pen that is 5 pixels wide as follows: private void Form1_Paint(object sender, PaintEventArgs e) Pen variable_name = new Pen ( color, width) A Pen object may be quite easily created as follows: A Graphics Object is of little use without a Pen object with which to draw (much as a sheet of paper is no good without a pen or pencil).
