Draw text filled with random colored circles in C#

This example shows how to draw text filled with random colored circles.

The example Draw text filled with random colored lines in C# shows how to draw text filled with random colored lines. This example is similar except it fills the text with random colored circles instead of lines.

After creating the clipping region (see the previous example for details), the program uses the following code to draw the random circles.

// Restrict drawing to the path.
using (Region clip_region = new Region(path))
{
    e.Graphics.Clip = clip_region;

    // Fill the path with circles.
    Random rand = new Random();
    for (int i = 1; i < 200; i++)
    {
        int radius = rand.Next(5, 50);
        int cx = rand.Next(0, ClientSize.Width);
        int cy = rand.Next(0, ClientSize.Height);
        using (Brush colored_brush = new SolidBrush(RandomColor()))
        {
            e.Graphics.FillEllipse(colored_brush,
                cx - radius, cy - radius, 2 * radius, 2 * radius);
        }
    }

    // Reset the clipping region.
    e.Graphics.ResetClip();
}

The code sets the Grahics object’s Clip property to the region defined by the text. It then generates random radius, X coordinate, and Y coordinate values for each circle. It calls the RandomColor method (used in the previous example) to pick a random color for the circle and fills the circle.

Download the example to see additional details.


Download Example   Follow me on Twitter   RSS feed


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

Leave a Reply

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