1: /// <summary>
2: /// Adjusts the Gamma
3: /// </summary>
4: /// <param name="OriginalImage">Image to change</param>
5: /// <param name="Value">Used to build the gamma ramp (usually .2 to 5)</param>
6: /// <returns>A bitmap object</returns>
7: public static Bitmap AdjustGamma(Bitmap OriginalImage, float Value)
8: {
9: Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
10: BitmapData NewData = Image.LockImage(NewBitmap);
11: BitmapData OldData = Image.LockImage(OriginalImage);
12: int NewPixelSize = Image.GetPixelSize(NewData);
13: int OldPixelSize = Image.GetPixelSize(OldData);
14:
15: int[] RedRamp = new int[256];
16: int[] GreenRamp = new int[256];
17: int[] BlueRamp = new int[256];
18: for (int x = 0; x < 256; ++x)
19: {
20: RedRamp[x] = MathHelper.Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
21: GreenRamp[x] = MathHelper.Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
22: BlueRamp[x] = MathHelper.Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
23: }
24:
25: for (int x = 0; x < NewBitmap.Width; ++x)
26: {
27: for (int y = 0; y < NewBitmap.Height; ++y)
28: {
29: Color Pixel = Image.GetPixel(OldData, x, y, OldPixelSize);
30: int Red = RedRamp[Pixel.R];
31: int Green = GreenRamp[Pixel.G];
32: int Blue = BlueRamp[Pixel.B];
33: Image.SetPixel(NewData, x, y, Color.FromArgb(Red, Green, Blue), NewPixelSize);
34: }
35: }
36:
37: Image.UnlockImage(NewBitmap, NewData);
38: Image.UnlockImage(OriginalImage, OldData);
39: return NewBitmap;
40: }