What is the best way to pick a random brush from the Brushes collection in C#?

SBurris picture SBurris · Jun 18, 2009 · Viewed 8.3k times · Source

What is the best way to pick a random brush from the System.Drawing.Brushes collection in C#?

Answer

jjxtra picture jjxtra · Jun 18, 2009

If you just want a solid brush with a random color, you can try this:

    Random r = new Random();
    int red = r.Next(0, byte.MaxValue + 1);
    int green = r.Next(0, byte.MaxValue + 1);
    int blue = r.Next(0, byte.MaxValue + 1);
    System.Drawing.Brush brush = new System.Drawing.SolidBrush(Color.FromArgb(red, green, blue));