在 C# 中从画笔集合中选择随机画笔的最佳方法是什么?
在 C# 中从 System.Drawing.Brushes 集合中选择随机画笔的最佳方法是什么?
What is the best way to pick a random brush from the System.Drawing.Brushes collection in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只想要一个具有随机颜色的实心画笔,您可以尝试以下操作:
If you just want a solid brush with a random color, you can try this:
对于 WPF,使用反射:
For WPF, use reflection:
我建议获取足够的样本画笔列表,然后从中随机选择。
仅仅获得一种随机颜色就会产生糟糕的颜色,您可以轻松地设置一个大约 50 种颜色的列表,然后您可以在每次需要随机颜色时使用。
I suggest getting a list of enough sample brushes, and randomly selecting from there.
Merely getting a random colour will yield terrible colours, and you could easily set up a list of maybe 50 colours that you could then use every time you need a random one.
一个明显的方法是生成一个随机数,然后选择相应的画笔。
An obvious way is to generate a random number and then pick the corresponding brush.