使用GDI +和C#更改图像对比度 [英] Changing image contrast with GDI+ and C#
问题描述
我的问题如下:
我正在制作一个程序,该程序可以通过C#代码操纵亮度,伽玛和对比度.对于亮度和伽玛,可以.我已经通过网上找到的代码实现了这一目标,但是我无法形成对比.
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.
我到目前为止唯一发现的是一个CalculateRamp
方法,该方法具有输入参数(double level, double brightness, double gamma, double 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.
此方法的另一个问题是,当我将带有随机level
参数的计算得出的渐变作为参数传递给SetDeviceGammaRamp(IntPtr hDC,ref RAMP rmp)
时,它实际上会改变屏幕对比度,但是当我移动亮度滑块时,对比度会产生变化滑块丢失.可能是因为使用了相同的方法,或者我不确定是什么原因.
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.
这是CalculateRamp
方法的代码,因为我调用了函数SetDeviceGammaRamp(...)
,以使用当前计算出的斜率来操纵对比度.我不确定是否必须以这种方式使用它:
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.
这篇关于使用GDI +和C#更改图像对比度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!