Edit

Share via


How to: Apply Gamma Correction to a Gradient

You can enable gamma correction for a linear gradient brush by setting the brush's GammaCorrection property to true. You can disable gamma correction by setting the GammaCorrection property to false. Gamma correction is disabled by default.

Example

The following example is a method that is called from a control's Paint event handler. The example creates a linear gradient brush and uses that brush to fill two rectangles. The first rectangle is filled without gamma correction, and the second rectangle is filled with gamma correction.

The following illustration shows the two filled rectangles. The top rectangle, which does not have gamma correction, appears dark in the middle. The bottom rectangle, which has gamma correction, appears to have more uniform intensity.

Two gradient-filled rectangles, with and without gamma correction.

public void FillTwoRectangles(PaintEventArgs e)
{
   LinearGradientBrush linGrBrush = new LinearGradientBrush(
       new Point(0, 10),
       new Point(200, 10),
       Color.Red,
       Color.Blue);

    e.Graphics.FillRectangle(linGrBrush, 0, 0, 200, 50);
    linGrBrush.GammaCorrection = true;
    e.Graphics.FillRectangle(linGrBrush, 0, 60, 200, 50);
}
Dim linGrBrush As New LinearGradientBrush( _
   New Point(0, 10), _
   New Point(200, 10), _
   Color.Red, _
   Color.Blue)

e.Graphics.FillRectangle(linGrBrush, 0, 0, 200, 50)
linGrBrush.GammaCorrection = True
e.Graphics.FillRectangle(linGrBrush, 0, 60, 200, 50)

Compiling the Code

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler.

See also