Problem in generating random colors - asp.net and c#

前端 未结 4 603
走了就别回头了
走了就别回头了 2021-02-10 04:40

I need to generate to generate random colors in Hex values in my asp.net application to draw a graph .

 Random random = new Random();
 color = String.Format(\"#         


        
4条回答
  •  滥情空心
    2021-02-10 05:24

    Try

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

    --EDIT--

    Update upon mjv's comment:

    public class RandomColor
    {
        Random _random;
        public RandomColor()
        {
            _random = new Random();
        }
    
        public Color GetNext()
        {
            return Color.FromArgb(_random.Next(0, 255), _random.Next(0, 255), _random.Next(0, 255));
        }
    }
    

    Usage:

    colorRandom.GetNext();
    

提交回复
热议问题