Drawing Other Graphics Shapes by Applying Cap and Dashed Line Styles in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

In this article we will create a pen, set its line cap and line dash styles, and use it- but this time, drawing graphics shapes, rather than simple lines.

Listing 4.21 creates several pens and uses them to draw an arc, Brezier curve, rectangle, and ellipse with the help of the DrawArc, DrawBezier, DrawRectangle, and DrawEllipse methods of the Graphics class.

LISTING 4.21: Using different pens to draw various graphics objects

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);
            Pen redPen = new Pen(new SolidBrush(Color.Red), 4);
            Pen bluePen = new Pen(new SolidBrush(Color.Blue), 5);
            Pen blackPen = new Pen(new SolidBrush(Color.Black), 3);

            //Set line styles
            redPen.DashStyle = DashStyle.Dash;
            redPen.SetLineCap(LineCap.DiamondAnchor, LineCap.ArrowAnchor, DashCap.Flat);
            bluePen.DashStyle = DashStyle.DashDotDot;
            bluePen.StartCap = LineCap.Triangle;
            bluePen.EndCap = LineCap.Triangle;
            bluePen.DashCap = DashCap.Triangle;
            blackPen.DashStyle = DashStyle.Dot;
            blackPen.DashOffset = 3.4F;
            blackPen.SetLineCap(LineCap.RoundAnchor, LineCap.Square, DashCap.Round);

            //Draw objects
            g.DrawArc(redPen, 10.0F, 10.0F, 50, 100, 45.0F, 90.0F);
            g.DrawRectangle(bluePen, 60, 80, 140, 50);
            g.DrawBezier(blackPen, 20.0F, 30.0F, 100.0F, 200.0F, 40.0F, 400.0F, 100.0F, 200.0F);
            g.DrawEllipse(redPen, 50, 50, 200, 100);

            //Dispose of objects
            redPen.Dispose();
            bluePen.Dispose();
            blackPen.Dispose();
            g.Dispose();
        }
    }
}

Figure 4.26 shows the output of Listing 4.21. All of the elements drawn have line cap and dash styles.

Transformation with Pens

Transformation is the process of changing graphics objects from one state to another. Rotation, scaling, reflection, translation, and shearing are examples to transform.

Figure 4.26.jpg

FIGURE 4.26: Graphics shapes with cap and dash styles

The Pen class provides methods for transformation and rotation. The RotateTransform method rotates a transformation by an angle. This method takes a rotation angle of type float. The second argument, MatrixOrder, is an optional parameter that provides an order for matrix transformation operations. The MatrixOrder enumeration defines the matrix order, which has two members: Append and Prepend. The matrix order is the order in which a matrix is multiplied with other matrices.

The difference between Append and Prepend is the order of the operation. For example, if two operations are participating in a process, the second operation will be performed after the first when the matrix order is Append; when the order is Prepend, the second operation will be performed before the first.

The MultiplyTransofrm method multiplies a transformation matrix by pen. Its first argument is a Matrix object, and the optional second argument is the matrix order of types MatrixOrder enumeration.

The TranslateTransform method of the Pen class translates a transformation by the specified dimension. This method takes two float type values for translation in x and y, and an optional third parameter of type MatrixOrder.

Listing 4.22 uses the ScaleTransofrm and RotateTransform methods to apply rotation on pens and rectangles.

LISTING 4.22: Applying transformation on pens

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);

            //Create a Pen Object
            Pen bluePen = new Pen(Color.Blue, 10);
            Pen redPen = new Pen(Color.Red, 5);

            //Supply rotate and scale transformations
            bluePen.ScaleTransform(3, 1);
            g.DrawEllipse(bluePen, 20, 20, 100, 50);
            g.DrawRectangle(redPen, 20, 120, 100, 50);
            bluePen.RotateTransform(90, MatrixOrder.Append);
            redPen.ScaleTransform(4, 2, MatrixOrder.Append);
            g.DrawEllipse(bluePen, 220, 20, 100, 50);
            g.DrawRectangle(redPen, 220, 120, 100, 50);

            //Dispose of objects
            redPen.Dispose();
            bluePen.Dispose();
            g.Dispose();
        }
    }
}

Figure 4.27 shows the output from Listing 4.22. The first ellipse and rectangle are drawn normally. The second ellipse and rectangle are drawn after rotation and scaling have been applied to their pens.

Figure 4.27.jpg

FIGURE 4.27: Rotation and scaling

Note: You need to reference the System.Drawing.Drawing2D namespace in order to run the code in the listings of this section because the Matrix class and the MatrixOrder enumeration are defined in this namespace.

Conclusion

Hope the article would have helped you in understanding Drawing Other Graphics Shapes by Applying Cap and Dashed Line Styles in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.