Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query GetMonitorBrightness from C#

How does GetMonitorBrightness http://msdn.microsoft.com/en-us/library/ms775205.aspx work? Can someone give me an actual working implementation calling this code in C#?

I'm trying to retrieve the allowed brightness levels my laptop supports.

I have the following working code that sets the brightness from 1 to ~150. But I'm looking for the allowed input values (min max values).

    static void SetBrightness(byte targetBrightness)
    {
        ManagementScope scope = new ManagementScope("root\\WMI");
        SelectQuery query = new SelectQuery("WmiMonitorBrightnessMethods");
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
        {
            using (ManagementObjectCollection objectCollection = searcher.Get())
            {
                foreach (ManagementObject mObj in objectCollection)
                {
                    mObj.InvokeMethod("WmiSetBrightness",
                        new Object[] { UInt32.MaxValue, targetBrightness });
                    break;
                }
            }
        }
    }
like image 954
CodingBarfield Avatar asked Mar 22 '23 21:03

CodingBarfield


1 Answers

While using Interop should be possible this function is also available through WMI. Changing my original code a bit resulted in the following code that should work:

 ManagementScope scope;
 SelectQuery query;

 scope = new ManagementScope("root\\WMI");
 query = new SelectQuery("SELECT * FROM WmiMonitorBrightness");

 using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
 {
    using (ManagementObjectCollection objectCollection = searcher.Get())
    {
      foreach (ManagementObject mObj in objectCollection)
      {
        Console.WriteLine(mObj.ClassPath);
        foreach (var item in mObj.Properties)
        {
          Console.WriteLine(item.Name + " " +item.Value.ToString());
          if(item.Name =="CurrentBrightness")
            //Do something with CurrentBrightness
        }
      }
    }
  }

Now I'm really curious how to handle 'special' cases like non laptop Screen's and if they implement any way to influence brightness.

like image 183
CodingBarfield Avatar answered Apr 19 '23 15:04

CodingBarfield