4

I'm looking for a way to get the color that windows 10 chooses automatically depending on the background image as shown below.

enter image description here

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>

master_ruko
  • 629
  • 1
  • 4
  • 22
  • I strongly suggest including the actual exception type+message in questions you post at stackoverflow. It shows that you have put some effort into diagnosing the problem, and avoids the chance to overlook something obvious. – C.Evenhuis Sep 12 '19 at 07:05
  • Could you post the inner exception and stack trace as well? – Emond Sep 12 '19 at 07:14
  • @Erno de Weerd I am a pretty novice coder and am not sure what an inner exception is but I'm adding the full details now. – master_ruko Sep 12 '19 at 07:17
  • Does this answer your question? [C# console get Windows 10 Accent Color](https://stackoverflow.com/questions/50840395/c-sharp-console-get-windows-10-accent-color) – GSerg Jan 15 '23 at 19:38

1 Answers1

5

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)
    ));
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Noorul
  • 873
  • 2
  • 9
  • 26