Setting brightness and contrast of the Windows Form

I'm working on an application, where the user has a requirement to set the brightness / contrast of the screen to arbitrary settings (day/night mode), we used an external program until now, but it's not a reliable solution (DarkFix.exe).

My idea is to somehow catch the form paint event when the form has the screen image in a bitmap (we already use doublebuffering), use a brightness/contrast filter on it, and then give it back to the painting routine. It is possible?

Or can I set the display somehow?
Any solutions for this problem?

.NET 2.0, C#

Thanks in advance
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It sounds like you're definitely on the right track.
Override the form's OnPaintBackground method, alter the bitmap, then draw it manually with
subiczAuthor Commented:
I found a solution:

this CalculateRamp will calculate a ramp for you with given level, gamma, brightness and contrast,
you can use this ramp later with SetDeviceGammaRamp WinAPI call.

// level should be between 2 and 100
// gamma should be between 2 and 50
// brigntess should be between 0 and 100
// contrast should be between 0 and 100
public RAMP CalculateRamp (double level, double gamma, double brightness, double contrast)
        RAMP ramp = new RAMP()
        ramp.Red = new ushort[256];
        ramp.Green = new ushort[256];
        ramp.Blue = new ushort[256];
        gamma /= 10;
        brightness = 1 + (((brightness - 50) / 100) * 65535);
        contrast = 1 + ((contrast - 50) / 100);
        level = 1 + ((level - 50) / 100);
        for (int i = 0;i < 256; i++)
                double value = i * 256;
                value = (Math.Pow(value / 65535, 1 / gamma) * 65535) + 0.5;
                value = ((((value / 65535) - 0.5) * contrast) + 0.5) * 65535;
                value = value += brightness;
                value *= level;
                ramp.Red[i] = ramp.Green[i] = ramp.Blue[i] = (ushort) Math.Min((double) 65535, Math.Max((double) 0, value));
        return ramp;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Keith DavisCommented:
How would you calculate the input values (arguments) for this method directly from the existing Gamma Ramp? In other words, what would be the reverse method?

If I am going to provide these utilities to the user, I need to present them with their current settings prior to their making the adjustments.

I have tried for three days to come up with a solution, and must admit that this is beyond my capabilities.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.