[C#]
using (Bitmap bm = (Bitmap)Bitmap.FromFile(Server.MapPath("rez/felix-tchverkin-7pkN83wDZwY-unsplash.jpg"))) {
bm.PixelFormat = new PixelFormat(bm.PixelFormat, ColorChannel.Alpha, false);
Effects.AdjustColors(bm, Color.White, (color, context) => {
const int tolerance = 50 * 50;
int dr = color.R - context.R;
int dg = color.G - context.G;
int db = color.B - context.B;
int delta = (dr * dr) + (dg * dg) + (db * db);
color.A = delta < tolerance ? (byte)0x00 : (byte)0xFF;
return color;
});
using (Bitmap bm2 = new Bitmap(bm.Width, bm.Height)) {
bm2.SetResolution(bm.VerticalResolution, bm.HorizontalResolution);
using (Graphics gr = Graphics.FromImage(bm2)) {
gr.Clear(Color.Red);
gr.DrawImage(bm, bm2.Bounds);
}
bm2.Save(Server.MapPath("IG8_Effects_AdjustColors2.jpg"));
}
}
The code above selects all the white - or nearly white - pixels
in the image and replaces them with red ones before saving the
final output as a JPEG. The final output is shown below.

felix-tchverkin-7pkN83wDZwY-unsplash.jpg

IG8_Effects_AdjustColors2.jpg
|