• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3777
  • Last Modified:

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,
0
Abu Hamdan
Asked:
Abu Hamdan
  • 3
  • 3
1 Solution
 
dunglaCommented:
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.
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor Commented:
Thanks,

But how this could be done using C#?? i have no idea
0
 
dunglaCommented:
Check out http://www.pinvoke.net to know how to call a function from Windows API.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
dunglaCommented:
This is more detail about DeviceIoControl function

http://pinvoke.net/default.aspx/kernel32/DeviceIoControl.html
0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor 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?


0
 
Abu HamdanEnterprise Architect, PM ExpertAuthor 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 :)
0
 
Computer101Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now