Serge's World

Blogging about software development, astronomy, genealogy and more.

Image Processing in C#: Part 3 - Adjusting the Brightness

To increase the brightness of an image, all you need to do is add a value to each component for each pixel in the image.

Increasing the brightness is not always reversible, because as the colour value tops out at 255, if the increased value goes above this value, it will be limited to that value. This makes any reversal of the process impossible. The opposite is true for negative values. Then if the value falls below 0, then the values are limited.

Brightness

public void ApplyBrightness(int brightness)
{
	int A, R, G, B;
	Color pixelColor;

	for (int y = 0; y < bitmapImage.Height; y++)
	{
		for (int x = 0; x < bitmapImage.Width; x++)
		{
			pixelColor = bitmapImage.GetPixel(x, y);
			A = pixelColor.A;
			R =  pixelColor.R + brightness;
			if (R > 255)
			{
				R = 255;
			}
			else if (R < 0)
			{
				R = 0;
			}

			G = pixelColor.G + brightness;
			if (G > 255)
			{
				G = 255;
			}
			else if (G < 0)
			{
				G = 0;
			}

			B = pixelColor.B + brightness;
			if (B > 255)
			{
				B = 255;
			}
			else if (B < 0)
			{
				B = 0;
			}

			bitmapImage.SetPixel(x, y, Color.FromArgb(A, R, G, B));
		}
	}

}

The full source used in the series is available from https://github.com/sjmeunier/image-processor.

Originally posted on my old blog, Smoky Cogs, on 19 Nov 2009

Tag Cloud

Algorithms (3) Android (10) Astronomy (25) Audio (1) Audiobooks (1) Barcodes (9) C# (69) Css (1) Deep sky (6) Esoteric languages (3) Family (3) Fractals (10) Gaming (1) Genealogy (14) General (2) Geodesy (3) Google (1) Graphics (3) Hubble (2) Humour (1) Image processing (23) Java (8) Javascript (5) jQuery (3) Jupiter (3) Maths (22) Moon (5) Music (4) Pets (5) Programming (88) Saturn (1) Science (1) Spitzer (4) Sun (4) Tutorials (68) Unity (3) Web (9) Whisky (13) Windows (1) Xamarin (2)