2 using System.Collections.Generic;
6 using System.Runtime.InteropServices;
7 using System.Drawing.Imaging;
8 using System.Drawing.Drawing2D;
15 public static class ExtensionesImages
25 public static Bitmap __ChangePixelColor(
this Bitmap scrBitmap, Color tcOldColor, Color tcNewColor)
30 Bitmap newBitmap =
new Bitmap(scrBitmap.Width, scrBitmap.Height);
31 for (
int i = 0; i < scrBitmap.Width; i++)
33 for (
int j = 0; j < scrBitmap.Height; j++)
36 lcCurrentPixel = scrBitmap.GetPixel(i, j);
38 if (lcCurrentPixel.ToArgb() == tcOldColor.ToArgb())
41 if (lcCurrentPixel.A > 150)
43 newBitmap.SetPixel(i, j, tcNewColor);
47 newBitmap.SetPixel(i, j, lcCurrentPixel);
62 public static Bitmap __ColorBalance(
this Bitmap sourceBitmap,
int redLevel,
int greenLevel,
int blueLevel)
64 if ((redLevel < 0 || redLevel > 255) || (greenLevel < 0 || greenLevel > 255) || (blueLevel < 0 || blueLevel > 255))
70 redLevel = 255 - redLevel;
71 greenLevel = 255 - greenLevel;
72 blueLevel = 255 - blueLevel;
73 return sourceBitmap.__ColorBalance((byte)redLevel, (byte)greenLevel, (byte)blueLevel);
86 public static Bitmap __ColorBalance(
this Bitmap sourceBitmap, byte redLevel, byte greenLevel, byte blueLevel)
88 BitmapData sourceData = sourceBitmap.LockBits(
new Rectangle(0, 0,
89 sourceBitmap.Width, sourceBitmap.Height),
90 ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
92 byte[] pixelBuffer =
new byte[sourceData.Stride * sourceData.Height];
94 Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);
96 sourceBitmap.UnlockBits(sourceData);
102 float blueLevelFloat = blueLevel;
103 float greenLevelFloat = greenLevel;
104 float redLevelFloat = redLevel;
106 for (
int k = 0; k + 4 < pixelBuffer.Length; k += 4)
108 blue = 255.0f / blueLevelFloat * (float)pixelBuffer[k];
109 green = 255.0f / greenLevelFloat * (float)pixelBuffer[k + 1];
110 red = 255.0f / redLevelFloat * (float)pixelBuffer[k + 2];
112 if (blue > 255) { blue = 255; }
113 else if (blue < 0) { blue = 0; }
115 if (green > 255) { green = 255; }
116 else if (green < 0) { green = 0; }
118 if (red > 255) { red = 255; }
119 else if (red < 0) { red = 0; }
121 pixelBuffer[k] = (byte)blue;
122 pixelBuffer[k + 1] = (byte)green;
123 pixelBuffer[k + 2] = (byte)red;
126 Bitmap resultBitmap =
new Bitmap(sourceBitmap.Width, sourceBitmap.Height);
128 BitmapData resultData = resultBitmap.LockBits(
new Rectangle(0, 0,
129 resultBitmap.Width, resultBitmap.Height),
130 ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
132 Marshal.Copy(pixelBuffer, 0, resultData.Scan0, pixelBuffer.Length);
133 resultBitmap.UnlockBits(resultData);