Draw striped lines in C#

striped lines

You can use the Pen class to draw striped lines. That class’s CompoundArray property stores an array of floats that gives the fraction of the line’s width that is drawn and skipped. For example, the array 0.0, 0.25, 0.75, 1.0 means draw the first 25% of the line’s width, skip to 75% of the way through, and then draw the last 25% of the line. The result is a line with a clear stripe down the middle.

This example uses the following code to draw a line that has two thin clear strips near its edges.

// Draw with a compound pen.
private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

    using (Pen the_pen = new Pen(Color.Blue, 20))
    {
        the_pen.CompoundArray =
            new float[] { 0.0f, 0.1f, 0.2f, 0.8f, 0.9f, 1.0f };

        // Draw the star.
        Rectangle rect = new Rectangle(
            35, 40,
            ClientSize.Width - 70,
            ClientSize.Height - 50);
        DrawStar(e.Graphics, the_pen,
            Brushes.White, 5, 2, rect);
    }
}

See the example Draw a star with a given number of points in C# to learn how the DrawStar method works.


Download Example   Follow me on Twitter   RSS feed




This entry was posted in drawing, graphics and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *