简体   繁体   English

创建随机颜色(System.Drawing.Color)

[英]Creating random colors (System.Drawing.Color)

I am tring to create random drawing colors. 我想创建随机绘图颜色。 There is an error. 有一个错误。 Could you help me about this code. 你能帮我解决一下这段代码吗?

        private Random random;

        private void MainForm_Load(object sender, EventArgs e)
        {
            random = new Random();
        }

        private Color GetRandomColor()
        {
            return Color.FromArgb(random.Next(0, 255), random.Next(0,255),random.Next(0,255));
        // The error is here
        }  

        public SolidBrush brushGet()
        {
            SolidBrush oBrush = new SolidBrush(GetRandomColor());
            return oBrush;
        }

This Worked for me (at the GetRandomColor): 这适用于我(在GetRandomColor):

Random random = new Random();
return Color.FromArgb((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));

I don't see any problems with the above code, other than the Random object not being initialized before it is called to. 我没有看到上述代码有任何问题,除了在调用之前没有初始化Random对象。 There is also absolutely no need to initialize it in the Load event of the form; 也绝对不需要在表单的Load事件中初始化它; it can be done right when it's declared: 它可以在声明时正确完成:

private static readonly Random Random = new Random();

Personally I'd not declare it on local scope, as far as I know you end up with the same value every time if you go about it that way. 就个人而言,我不会在本地范围内声明它,据我所知,如果你这样做,你每次都会得到相同的价值。 I also personally don't see the need of overcomplicating things; 我个人也不认为需要过于复杂的事情; generating random numbers everytime and using the Color.FromAgb() method you should be fine. 每次生成随机数并使用Color.FromAgb()方法你应该没问题。

    private Color color;
    private int b;
    public Color Random()
    {
        Random r = new Random();
        b = r.Next(1, 5);
        switch (b)
        {
            case 1:
                {
                    color = Color.Red;
                }
                break;
            case 2:
                {
                    color = Color.Blue;
                }
                break;
            case 3:
                {
                    color = Color.Green;
                }
                break;
            case 4:
                {
                    color = Color.Yellow;
                }
                break;
        }

        return color;
    }

The error is here 错误在这里

return Color.FromArgb(random.Next(0,255), random.Next(0,255), random.Next(0,255));

You need to cast random.Next(0, 255) doing this (byte)random.Next(0, 255) , and FromArgb needs 4 parameters. 你需要转换random.Next(0, 255)执行this (byte)random.Next(0, 255)FromArgb需要4个参数。

Generating 3 random numbers and calculating the color integer can be avoided: 可以避免生成3个随机数并计算颜色整数:

static Random random = new Random();

Color GetRandomColor() { return Color.FromArgb(unchecked(random.Next() | 255 << 24)); }

Color GetRandomKnownColor() {
    return Color.FromKnownColor((KnownColor)random.Next((int)KnownColor.YellowGreen) + 1); }

The unchecked | 255 << 24 unchecked | 255 << 24 unchecked | 255 << 24 part is used to avoid transparent colors. unchecked | 255 << 24部分用于避免透明颜色。

MainForm_Load可以创建random之前调用brushGet

using Microsoft.VisualBasic.PowerPacks;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Random random = new Random();

        public Form1()
        {    
            InitializeComponent();
        }

        private void ovalShape1_Click(object sender, EventArgs e)
        {        
            ovalShape1.BackStyle = BackStyle.Opaque;

            random = new Random();

            ovalShape1.BackColor = Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));

         }
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM