Link to home
Create AccountLog in
Avatar of Nasser Hamdan
Nasser HamdanFlag for United Arab Emirates

asked on

C# control screen brightness

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,
Avatar of dungla
dungla
Flag of Viet Nam image

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.
Avatar of Nasser Hamdan

ASKER

Thanks,

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

http://pinvoke.net/default.aspx/kernel32/DeviceIoControl.html
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?


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 :)
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer