Nasser Hamdan
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,
Is there a way using C# that i can control the screen brightness? if yes does anyone have a sample code for this?
Thanks,
ASKER
Thanks,
But how this could be done using C#?? i have no idea
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
http://pinvoke.net/default.aspx/kernel32/DeviceIoControl.html
ASKER
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.SafeHandle s.SafeFile Handle hDevice,
EIOControlCode IoControlCode,
[MarshalAs(UnmanagedType.A sAny)]
[In] object InBuffer,
uint nInBufferSize,
[MarshalAs(UnmanagedType.A sAny)]
[out] object OutBuffer,
uint nOutBufferSize,
ref uint pBytesReturned,
[In] ref System.Threading.NativeOve rlapped Overlapped
Okay i have included this on my application,, so what is next? how can i use it with the brightness thing?
[DllImport("Kernel32.dll",
public static extern bool DeviceIoControl(
Microsoft.Win32.SafeHandle
EIOControlCode IoControlCode,
[MarshalAs(UnmanagedType.A
[In] object InBuffer,
uint nInBufferSize,
[MarshalAs(UnmanagedType.A
[out] object OutBuffer,
uint nOutBufferSize,
ref uint pBytesReturned,
[In] ref System.Threading.NativeOve
Okay i have included this on my application,, so what is next? how can i use it with the brightness thing?
ASKER
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.S equential, CharSet = CharSet.Ansi)]
public struct RAMP
{
[MarshalAs(UnmanagedType.B yValArray, SizeConst = 256)]
public UInt16[] Red;
[MarshalAs(UnmanagedType.B yValArray, SizeConst = 256)]
public UInt16[] Green;
[MarshalAs(UnmanagedType.B yValArray, SizeConst = 256)]
public UInt16[] Blue;
}
int _gammaValue;
public ScreenBrightness()
{
InitializeComponent();
}
public ScreenBrightness(IContaine r 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.ToDoubl e((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(I ntPtr.Zero ), ref ramp);
}
}
}
Just create new C# component class and paste this code on :)
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.S
public struct RAMP
{
[MarshalAs(UnmanagedType.B
public UInt16[] Red;
[MarshalAs(UnmanagedType.B
public UInt16[] Green;
[MarshalAs(UnmanagedType.B
public UInt16[] Blue;
}
int _gammaValue;
public ScreenBrightness()
{
InitializeComponent();
}
public ScreenBrightness(IContaine
{
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.ToDoubl
_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(I
}
}
}
Just create new C# component class and paste this code on :)
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
through the P/Invoke layer in the Windows API, and pass
IOCTL_VIDEO_SET_DISPLAY_BR
Hope this helps.