We help IT Professionals succeed at work.

C# control screen brightness

4,463 Views
Last Modified: 2013-11-06
Hi,

Is there a way using C# that i can control the screen brightness? if yes does anyone have a sample code for this?

Thanks,
Comment
Watch Question

Commented:
If the display supports it, you can call the DeviceIoControl function
through the P/Invoke layer in the Windows API, and pass
IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS for the dwControlCode parameter.

Hope this helps.
Abu HamdanEnterprise Architect, PM Expert

Author

Commented:
Thanks,

But how this could be done using C#?? i have no idea

Commented:
Check out http://www.pinvoke.net to know how to call a function from Windows API.

Commented:
This is more detail about DeviceIoControl function

http://pinvoke.net/default.aspx/kernel32/DeviceIoControl.html
Abu HamdanEnterprise Architect, PM Expert

Author

Commented:
I have looked to the site,, i dont see sample for sending the brightness parameter ?


    [DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)]
    public static extern bool DeviceIoControl(
        Microsoft.Win32.SafeHandles.SafeFileHandle hDevice,
        EIOControlCode IoControlCode,
        [MarshalAs(UnmanagedType.AsAny)]
        [In] object InBuffer,
        uint nInBufferSize,
        [MarshalAs(UnmanagedType.AsAny)]
        [out] object OutBuffer,
        uint nOutBufferSize,
        ref uint pBytesReturned,
        [In] ref System.Threading.NativeOverlapped Overlapped
   
Okay i have included this on my application,, so what is next? how can i use it with the brightness thing?


Abu HamdanEnterprise Architect, PM Expert

Author

Commented:
I actually found a solution for this ,, i will post so others can benefit from :)

 public partial class ScreenBrightness : Component
    {
        [DllImport("gdi32.dll")]
        public static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);

        RAMP ramp = new RAMP();

        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr hWnd);


        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct RAMP
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
            public UInt16[] Red;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
            public UInt16[] Green;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
            public UInt16[] Blue;
        }
        int _gammaValue;

        public ScreenBrightness()
        {
            InitializeComponent();
        }

        public ScreenBrightness(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }


        [Description("Brightness Gamma Value")]
        [Category("Brightness")]
        public int SetGammaValue
        {
            get
            {
                return _gammaValue;
            }
            set
            {
                _gammaValue = value;
            }
        }


        /// <summary>
        /// Apply the selected gamma value to screen
        /// </summary>
        public void ApplyGamma()
        {
            // since gamma value is max 44 ,, we need to take the percentage from this because
            // it needed from 0 - 100%
            double gValue = _gammaValue;
            gValue = Math.Floor(Convert.ToDouble((gValue / 2.27)));

            _gammaValue = Convert.ToInt16(gValue);  

            if(_gammaValue!=0)
            {
                ramp.Red = new ushort[256];
                ramp.Green = new ushort[256];
                ramp.Blue = new ushort[256];

                for (int i = 1; i < 256; i++)
                {
                    // gamma is a value between 3 and 44
                    ramp.Red[i] = ramp.Green[i] = ramp.Blue[i] = (ushort)(Math.Min(65535, Math.Max(0, Math.Pow((i + 1) / 256.0, (_gammaValue + 5) * 0.1) * 65535 + 0.5)));

                }

                SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref ramp);
            }
        }
    }


Just create new C# component class and paste this code on :)
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.