I was trying to make an eye saver program myself in c# that is supposed to darken the screen after some time of work. In order to change the screen brightness I followed this (https://www.codeproject.com/Articles/47355/Setting-Screen-Brightness-in-C) article that uses SetDeviceGammaRamp method. My code is the following:
private unsafe void dimScreen()
{
var brightness = 10;
short* gArray = stackalloc short[3 * 256];
short* idx = gArray;
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 256; i++)
{
int arrayVal = i * (brightness + 128);
if (arrayVal > 65535)
arrayVal = 65535;
*idx = (short)arrayVal;
idx++;
}
}
SetDeviceGammaRamp(hdc, gArray);
Thread.Sleep(10000);
}
However, instead of changing the brightness permanently (or at least for 10 seconds) the screen just blinks for half a second. Calling SetDeviceGammaRamp in cycle with a sleep several times doesn't change the situation, all I get is just several such blinks. If I change the brightness variable the brightness of that blink changes as well so I assume the hdc and gArray variables are assigned properly. I tried looking for other solutions but most of them use this method and no one seems to have this problem. Any ideas on what the issue might be?
UPD: It seems that the problem all along was with flux. It notices the change in gamma and resets it to the previous value.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…