Image Color Filter

bakes_buns

New member
Joined
Jan 22, 2003
Messages
2
Hi All,

With an image, how do I replace all pixels of a certain color with another color??

The image is created from a .gif file. I would prefer to change the color in the palette/color table if thats possible, rather than going through each pixel in the image and changing it.
 
Ive never tried it, but all Image objects contain a Palette property that has an Entries array (of Colors). Have you tried changing this? You may have to change the PixelFormat to something compatable with a Palette - a gif should probably always have a Palette so it may not be an issue unless you plan on loading a BMP in the future.

I think I heard that to change pixels individually, you have to use rectangles that are 1x1 pixel. I cant remember how to use it though :)

-nerseus
 
I tried using the code below to cycle through the palette enties and modify the appropriate color. But the R, G, B properties of the color object are read only, you cant replace them and it doesnt compile.

C#:
Color SHIRT_MASK = Color.FromArgb(200,100,200);

Color SHIRT_COLOR = Color.FromArgb(10,100,20);

foreach (Color color in pal.Entries)
{
  if (color.Equals(SHIRT_MASK))
  {
    color.R = SHIRT_COLOR.R;
    color.G = SHIRT_COLOR.G;
    color.B = SHIRT_COLOR.B;
  }
}
[edit]fixed code tags[/edit]Any other suggestions??
 
Last edited by a moderator:
Have a look at the ColorMap class, found in the System.Drawing.Imaging
namespace; it seems to be exactly what youre looking for.
 
For my suggestion, you may to set each color to a new Color rather than change the RGB individually. Try something like:
Code:
Color SHIRT_MASK = Color.FromArgb(200,100,200);

Color SHIRT_COLOR = Color.FromArgb(10,100,20);

foreach (Color color in pal.Entries)
{
  if (color.Equals(SHIRT_MASK))
  {
    color = SHIRT_COLOR;
  }
}

Since structs are value types, it will make a copy of your SHIRT_COLOR and put it in the "color" variable.

You can try Buckys suggestion, too. Ive no idea which is better as I dont really do much with palette-based images :)

-ner
 
Back
Top