Pen Alignment in GDI+


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

The alignment of a pen represents its position respective to a line. The PenAlignment enumeration specifies the alignment of a pen-meaning the center point of the pen width relative to the line. Table 4.11 describes the members of the PenAlignment enumeration.

To see alignment in action, let's create a sample application. We create a Windows application, and add a combo box, three labels, two buttons, and a numeric up-down control. We change the control properties, and the final form looks like Figure 4.21.

Figure 4.21.jpg

FIGURE 4.21: Our pen alignment application

TABLE 4.11: PenAlignment members
 
Member Description

Center

The pen is centered.

Inset

The pen is inside the line.

Left

The pen is left of the line.

Outset

The pen is outside of the line.

Right

The pen is right of the line.


The Pen Alignment combo box lists the alignment of a pen. Pen Width represents the width of the pen, and Pen Color lets you pick the color of the pen. The Pen Color button click event handler simply sets the color of the pen and stores the selected color in a Color type variable at the class level, as shown in Listing 4.17.

LISTING 4.17: The Pen Color button click event handler

        private Color penColor = Color.Red;
        private void ColorBtn_Click(object sender, System.EventArgs e)
        {
            //Use ColorDialog to select a color
            ColorDialog clrDlg = new ColorDialog();
            if (clrDlg.ShowDialog() == DialogResult.OK)
            {
                //Save color as background color
                //and fill text box with this color
                penColor = clrDlg.Color;
                 ColorBtn.BackColor = penColor;
            }
        }

Listing 4.18 (on the form's load event handler) loads all alignments to the combo box.

LISTING 4.18: Adding pen alignments to the combo box

        private void Form1_Load(object sender, System.EventArgs e)
        {
             AddPenAlignments();
        }

        private void AddPenAlignments()
        {
            //Add pen alignment
             comboBox1.Items.Add(PenAlignment.Center);
            comboBox1.Text =
                 PenAlignment.Center.ToString();
             comboBox1.Items.Add(PenAlignment.Inset);
             comboBox1.Items.Add(PenAlignment.Left);
             comboBox1.Items.Add(PenAlignment.Outset);
             comboBox1.Items.Add(PenAlignment.Right);
        }

Finally, in Listing 4.19 we write code for the Draw Graphics button click event handler. We set the Width and Color properties of the pen after reading values from the form's controls. Then we look for the current alignment set by the user in the combo box and set the Alignment property of the pen. In the end, we use this pen to draw a rectangle. We also fill one more rectangle with a linear gradient brush.

LISTING 4.19: Creating a pen with alignment

      private void DrawBtn_Click(object sender, System.EventArgs e)
      {
            //Create a Graphics object and set it clear
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);
            //Create a solid brush and a hatch brush
            Pen pn1 = new Pen(Color.Blue, 3);
            pn1.Width = (float)numericUpDown1.Value;
            pn1.Color = ColorBtn.BackColor;
            //Find out current pen alignment
            string str = comboBox1.Text;
            switch (str)
            {
                case "Center":
                     pn1.Alignment = PenAlignment.Center;
                    break;
                case "Inset":
                     pn1.Alignment = PenAlignment.Inset;
                    break;
                case "Left":
                     pn1.Alignment = PenAlignment.Left;
                    break;
                case "Outset":
                     pn1.Alignment = PenAlignment.Outset;
                    break;
                case "Right":
                     pn1.Alignment = PenAlignment.Right;
                    break;
                default:
                    break;
            }

            //Create a pen from a hatch brush
            //Draw a rectangle
             g.DrawRectangle(pn1, 80, 150, 150, 150);
            //Create a brush
             LinearGradientBrush brush =
            new LinearGradientBrush(
            new Rectangle(10, 10, 20, 20), Color.Blue,
                Color.Green, 45.0F);
             g.FillRectangle(brush, 90, 160, 130, 130);
            //Dispose of objects
            pn1.Dispose();
            g.Dispose();
        }

Figure 4.22 shows the output from Listing 4.19. The pen width is 10 and alignment is center.

If we set the alignment as inset, we get Figure 4.23.

Figure 4.22.jpg

FIGURE 4.22: Drawing with center pen alignment

Figure 4.23.jpg

FIGURE 4.23: Drawing with inset pen alignment

Conclusion

Hope the article would have helped you in understanding Pen Alignment 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.