【问题标题】:What is the best way to pick a random brush from the Brushes collection in C#?从 C# 中的画笔集合中选择随机画笔的最佳方法是什么?
【发布时间】:2010-11-03 21:11:02
【问题描述】:

在 C# 中从 System.Drawing.Brushes 集合中选择随机画笔的最佳方法是什么?

【问题讨论】:

    标签: c# collections select brush


    【解决方案1】:

    对于 WPF,使用反射:

    var r = new Random();
    var properties = typeof(Brushes).GetProperties();
    var count = properties.Count();
    
    var colour = properties
                .Select(x => new { Property = x, Index = r.Next(count) })
                .OrderBy(x => x.Index)
                .First();
    
    return (SolidColorBrush)colour.Property.GetValue(colour, null);
    

    【讨论】:

    • 他想要 System.Drawing.Brush
    【解决方案2】:

    我建议获取足够的示例画笔列表,并从中随机选择。

    仅仅获得一种随机颜色会产生糟糕的颜色,您可以轻松设置一个可能包含 50 种颜色的列表,然后在每次需要随机颜色时使用。

    【讨论】:

      【解决方案3】:

      如果你只想要一个随机颜色的纯色画笔,你可以试试这个:

          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));
      

      【讨论】:

        【解决方案4】:

        一个明显的方法是生成一个随机数,然后选择相应的画笔。

        【讨论】:

          猜你喜欢
          • 2011-08-30
          • 2010-09-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多