Adjust colors in a bitmap. Pixels are passed to the delegate function left to right, top to bottom until the entire area has been scanned.
Syntax

[C#]

static void AdjustColors(Bitmap bitmap, ProcessColor action);

[Visual Basic]

Shared Sub AdjustColors(bitmap As Bitmap, action As ProcessColor)
Params
Name Description
bitmap The bitmap to process
action The delegate for color processing.
Notes

None.

Example

[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 = 75 * 75;
    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;
  });
  EncoderParameters pars = new EncoderParameters() { UseAlpha = true };
  bm.Save(Server.MapPath("IG8_Effects_AdjustColors1.png"), ImageFormat.Png, pars);
}


The code above draws an image onto a bitmap. It then selects everything that is white and saves the completed image with selection-as-alpha-channel into a png. The input and output images are shown below.


felix-tchverkin-7pkN83wDZwY-unsplash.jpg


IG8_Effects_AdjustColors1.png

Example

[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