C# 隨機顏色的方法


 1          public string GetRandomColor()
 3         {
 4 
 5             Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
 6 
 7             //  對於C#的隨機數,沒什么好說的
 8 
 9             System.Threading.Thread.Sleep(RandomNum_First.Next(50));
10 
11             Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
12 
13  
14 
15             //  為了在白色背景上顯示,盡量生成深色
16 
17             int int_Red = RandomNum_First.Next(256);
18 
19             int int_Green = RandomNum_Sencond.Next(256);
20 
21             int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
22 
23             int_Blue = (int_Blue > 255) ? 255 : int_Blue;
24             Color color = Color.FromArgb(int_Red, int_Green, int_Blue);
25             string strColor = "#" + Convert.ToString(color.ToArgb(), 16).PadLeft(8, '0').Substring(2, 6);
26             return strColor;
27 
28         } 
29 
30 
31   

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM