Is there any way to get system's current brightness using c# program. I want to adjust system brightness by c# program. I tried so many ways but they are just for set system brightness and using a predefined value but i want to get current brightness and adjust accordingly. there is the code which i am using but not getting the required result.
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace BrightnessApp
{
public partial class Form1 : Form
{
private int _gammaValue;
private RAMP _ramp;
public Form1()
{
InitializeComponent();
}
public Form1(IContainer container)
{
container.Add(this);
InitializeComponent();
}
[Description("Brightness Gamma Value")]
[Category("Brightness")]
public int SetGammaValue
{
get { return _gammaValue; }
set { _gammaValue = value; }
}
private void Form1_Load(object sender, EventArgs e)
{
}
[DllImport("gdi32.dll")]
public static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
public void ApplyGamma()
{
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++)
{
_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);
}
}
#region Nested type: RAMP
[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;
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
_gammaValue += 10;
ApplyGamma();
}
private void button2_Click(object sender, EventArgs e)
{
_gammaValue--;
ApplyGamma();
}
}
}
If you have any better way please let me now. Thanks:)