Changing image contrast with GDI+ and C#
My problem is the following :
I am making a program, which can manipulate brightness, gamma and contrast through C# code. For brightness and gamma it is OK. I have achieved it through code I found in the net, but I can't for contrast.
The only thing I have found by now is a CalculateRamp
method, which has as input parameters (double level, double brightness, double gamma, double contrast)
. I know what input to give for brightness, gamma and contrast (开发者_高级运维the values from the sliders in the interface), but I don't know what level is for.
The other problem with this method is that when I pass the calculated ramp with random level
parameter as a parameter to SetDeviceGammaRamp(IntPtr hDC,ref RAMP rmp)
it actually changes the screen contrast, but when I move the brightness slider the changes made from the contrast slider are lost. It may be because of using the same method or I am not sure for what.
I will be very thankful to any help or ideas, no matter if they are changes to my current solution which is not full, or a brand new solution - which I would prefer - because I feel in some way unsure with this. Thanks in advance to everybody.
Here is the code of the CalculateRamp
method, as the function SetDeviceGammaRamp(...)
is called by me to manipulate the contrast with the current calculated ramp. I am not sure if I have to use it in this way or not:
public static void CalculateRamp(double level, double gamma, double brightness, double contrast)
{
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));
}
SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref ramp);
}
Try taking a look at this article. Its a color matrix which shows how to manipulate both the brightness and the contrast.
This article might also interest you, though I think the method used in this article is slower then the matrix.
精彩评论