title: Changing image contrast with GDI+ and C#
body: <p>My problem is the following :</p>
  
  <p>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. </p>
  
  <p>The only thing I have found by now is a <code>CalculateRamp</code> method, which has as input parameters <code>(double level, double brightness, double gamma, double contrast)</code>. 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. </p>
  
  <p>The other problem with this method is that when I pass the calculated ramp with random <code>level</code> parameter as a parameter to <code>SetDeviceGammaRamp(IntPtr hDC,ref RAMP rmp)</code> 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.</p>
  
  <p>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.</p>
  
  <p>Here is the code of the <code>CalculateRamp</code> method, as the function <code>SetDeviceGammaRamp(...)</code> 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:</p>
  
  <pre><code>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 &lt; 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);
  }
  </code></pre>