Setting brightness and contrast of the Windows Form

Posted on 2008-11-14
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
    LVL 7

    Expert Comment

    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

    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
    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    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.…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now