I'm looking for a way to get the color that windows 10 chooses automatically depending on the background image as shown below.
I tried searching, and found
var color = (Color)this.Resources["SystemAccentColor"];
and
var color = (Color)Application.Current.Resources["SystemAccentColor"];
but both thew an exception
System.Exception
HResult=0x8000FFFF
Message=Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
You will get only Hex Color in this code Application.Current.Resources["SystemAccentColor"]
You have to convert it into usable color format, here is the solution.
var color = Application.Current.Resources["SystemAccentColor"];
btnTest.Background = GetColorFromHex(color.ToString());
And here is the converting function
public static SolidColorBrush GetColorFromHex(string hexaColor)
{
return new SolidColorBrush(
Color.FromArgb(
Convert.ToByte(hexaColor.Substring(1, 2), 16),
Convert.ToByte(hexaColor.Substring(3, 2), 16),
Convert.ToByte(hexaColor.Substring(5, 2), 16),
Convert.ToByte(hexaColor.Substring(7, 2), 16)
)
);
}
Hope this helps,
User contributions licensed under CC BY-SA 3.0