Related Articles

Back to Latest Articles

C# Tutorial: How To Use Gamma Correction

Andraz Krzisnik
C# Tutorial: How To Use Gamma Correction

Gamma correction is a process, which allows you to set the brightness of screen. You can often run into this setting in video games, where you set the brightness of your screen based on an image. This image is usually just an intensity ramp or a logo, for which you set the brightness, so you can just barely see it.
Gamma correction works by changing the values of each pixel individually. Equation by which the values are transformed is, what we call, a power law equation.

Equation looks as following

Gamma correction equation

S represents a certain pixel in the output image. C is a constant, that is commonly set to 1. R represents a pixel in the input image, which is in the same position as S pixel. And finally γ (gamma) exponent is the value we are most interested in, as it is responsible for changing the brightness of our image.
If we set gamma to be less than 1 (e.g. 0.3, 0.5, 0.04), then our output image will appear brighter as before. And in the case we set it to be more than 1 (e.g. 1.5, 2.2, 25), our output image will become darker.

Gamma correction function in C#

private Bitmap GammaCorrection(Bitmap img, double gamma, double c = 1d)
    int width = img.Width;
    int height = img.Height;
    BitmapData srcData = img.LockBits(new Rectangle(0, 0, width, height),
        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    int bytes = srcData.Stride * srcData.Height;
    byte[] buffer = new byte[bytes];
    byte[] result = new byte[bytes];
    Marshal.Copy(srcData.Scan0, buffer, 0, bytes);
    int current = 0;
    int cChannels = 3;
    for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
            current = y * srcData.Stride + x * 4;
            for (int i = 0; i < cChannels; i++)
                double range = (double)buffer[current + i] / 255;
                double correction = c * Math.Pow(range, gamma);
                result[current + i] = (byte)(correction * 255);
            result[current + 3] = 255;
    Bitmap resImg = new Bitmap(width, height);
    BitmapData resData = resImg.LockBits(new Rectangle(0, 0, width, height),
        ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
    Marshal.Copy(result, 0, resData.Scan0, bytes);
    return resImg;

Entire project is available for download

Download Project



Show Comments (24)


  • is very informative, bookmarked

    • Article Author
  • cheap bouncy castles to buy

    Great weblog here! Additionally your website lots up fast!
    What host are you the usage of? Can I am getting your affiliate hyperlink
    for your host? I want my site loaded up as fast as
    yours lol

    • Article Author
  • test

    Hi there it’s me, I am also visiting this site daily, this web
    site is genuinely good and the visitors are actually sharing good thoughts.

    • Article Author

Related Articles

Frequency Domain Filtering

How To Use Laplacian Filter – C# Guide

We use Laplacian filter to sharpen images. We are going to talk about the details how exactly this filter works. In essence, it’s one of the highpass filters, which...

Posted on by Andraz Krzisnik
Mean Filters

How To Use Arithmetic Mean Filter On Images – C# Guide

Arithmetic mean filter is one of the simplest mean filters we could use to reduce noise from an image. Learn more about spatial filtering.

Posted on by Andraz Krzisnik