C#의 브러시 컬렉션에서 임의의 브러시를 선택하는 가장 좋은 방법은 무엇입니까?
-
06-07-2019 - |
문제
C#의 System.Drawing.Brushes Collection에서 임의의 브러시를 선택하는 가장 좋은 방법은 무엇입니까?
해결책
임의의 색상의 단단한 브러시를 원한다면 이것을 시도 할 수 있습니다.
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));
다른 팁
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);
충분한 샘플 브러시 목록을 얻고 거기에서 무작위로 선택하는 것이 좋습니다.
임의의 색상을 얻으면 끔찍한 색상을 얻을 수 있으며, 임의의 색상이 필요할 때마다 사용할 수있는 50 가지 색상 목록을 쉽게 설정할 수 있습니다.
명백한 방법은 난수를 생성 한 다음 해당 브러시를 선택하는 것입니다.
제휴하지 않습니다 StackOverflow