We help IT Professionals succeed at work.

C# control screen brightness

Last Modified: 2013-11-06

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

Watch Question

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



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

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

This is more detail about DeviceIoControl function

Abu HamdanEnterprise Architect, PM Expert


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,
        [In] object InBuffer,
        uint nInBufferSize,
        [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


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

 public partial class ScreenBrightness : Component
        public static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);

        RAMP ramp = new RAMP();

        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()

        public ScreenBrightness(IContainer container)


        [Description("Brightness Gamma Value")]
        public int SetGammaValue
                return _gammaValue;
                _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);  

                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)

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.


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.