• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1842
  • Last Modified:

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
0
subicz
Asked:
subicz
1 Solution
 
psdavisCommented:
It sounds like you're definitely on the right track.
Override the form's OnPaintBackground method, alter the bitmap, then draw it manually with
e.Graphics.DrawImage
0
 
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

0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now