Setting brightness and contrast of the Windows Form

Posted on 2008-11-14
Medium Priority
Last Modified: 2016-09-03
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
Question by:subicz

Expert Comment

ID: 22964638
It sounds like you're definitely on the right track.
Override the form's OnPaintBackground method, alter the bitmap, then draw it manually with

Accepted Solution

subicz earned 0 total points
ID: 22992241
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


Expert Comment

by:Keith Davis
ID: 41782974
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.

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question