clsImages.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Drawing;
6 using System.Runtime.InteropServices;
7 using System.Drawing.Imaging;
8 using System.Drawing.Drawing2D;
9 
10 namespace sage.ew.images.Clases
11 {
15  public static class ExtensionesImages
16  {
25  public static Bitmap __ChangePixelColor(this Bitmap scrBitmap, Color tcOldColor, Color tcNewColor)
26  {
27  //Contendra el color actual
28  Color lcCurrentPixel;
29  //make an empty bitmap the same size as scrBitmap
30  Bitmap newBitmap = new Bitmap(scrBitmap.Width, scrBitmap.Height);
31  for (int i = 0; i < scrBitmap.Width; i++)
32  {
33  for (int j = 0; j < scrBitmap.Height; j++)
34  {
35  //get the pixel from the scrBitmap image
36  lcCurrentPixel = scrBitmap.GetPixel(i, j);
37  //Comrprobamos si el color del pixel es el mismo que el que queremos cambiar
38  if (lcCurrentPixel.ToArgb() == tcOldColor.ToArgb())
39  {
40  // > 150 because.. Images edges can be of low pixel colr. if we set all pixel color to new then there will be no smoothness left.
41  if (lcCurrentPixel.A > 150)
42  {
43  newBitmap.SetPixel(i, j, tcNewColor);
44  continue;
45  }
46  }
47  newBitmap.SetPixel(i, j, lcCurrentPixel);
48  }
49  }
50  return newBitmap;
51  }
52 
62  public static Bitmap __ColorBalance(this Bitmap sourceBitmap, int redLevel, int greenLevel, int blueLevel)
63  {
64  if ((redLevel < 0 || redLevel > 255) || (greenLevel < 0 || greenLevel > 255) || (blueLevel < 0 || blueLevel > 255))
65  {
66  return sourceBitmap;
67  }
68  else
69  {
70  redLevel = 255 - redLevel;
71  greenLevel = 255 - greenLevel;
72  blueLevel = 255 - blueLevel;
73  return sourceBitmap.__ColorBalance((byte)redLevel, (byte)greenLevel, (byte)blueLevel);
74  }
75  }
76 
86  public static Bitmap __ColorBalance(this Bitmap sourceBitmap, byte redLevel, byte greenLevel, byte blueLevel)
87  {
88  BitmapData sourceData = sourceBitmap.LockBits(new Rectangle(0, 0,
89  sourceBitmap.Width, sourceBitmap.Height),
90  ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
91 
92  byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height];
93 
94  Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);
95 
96  sourceBitmap.UnlockBits(sourceData);
97 
98  float blue = 0;
99  float green = 0;
100  float red = 0;
101 
102  float blueLevelFloat = blueLevel;
103  float greenLevelFloat = greenLevel;
104  float redLevelFloat = redLevel;
105 
106  for (int k = 0; k + 4 < pixelBuffer.Length; k += 4)
107  {
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];
111 
112  if (blue > 255) { blue = 255; }
113  else if (blue < 0) { blue = 0; }
114 
115  if (green > 255) { green = 255; }
116  else if (green < 0) { green = 0; }
117 
118  if (red > 255) { red = 255; }
119  else if (red < 0) { red = 0; }
120 
121  pixelBuffer[k] = (byte)blue;
122  pixelBuffer[k + 1] = (byte)green;
123  pixelBuffer[k + 2] = (byte)red;
124  }
125 
126  Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);
127 
128  BitmapData resultData = resultBitmap.LockBits(new Rectangle(0, 0,
129  resultBitmap.Width, resultBitmap.Height),
130  ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
131 
132  Marshal.Copy(pixelBuffer, 0, resultData.Scan0, pixelBuffer.Length);
133  resultBitmap.UnlockBits(resultData);
134 
135  return resultBitmap;
136  }
137  }
138 }