Changing pixels color

Aug 3, 2010 at 5:21 PM
I'm just trying to get the black pixels from a png image and convert them to red pixels. However, it doesn't work. This is my code: WriteableBitmap wb = new WriteableBitmap(myImage, null); for(int y = 0 ; y < myImage.Height; y++) { for (int x=0; x < myImage.Width;x++) { Color color = wb.GetPixel(x, y); if( color == Colors.Black) wb.SetPixel(x,y,Colors.Red); } } myImagen.Source = wb; Can you give me a and on this ? Thanks
Coordinator
Aug 3, 2010 at 6:01 PM

Are you sure the color is perfect deep black? Colors.Black = 0xFF000000! You should debug your code and see what Color color = wb.GetPixel(x, y) returns. I guess it's not deep black, therefore you should rather check if the color is below a certain threshold.

In general I'd recommend to code it like this using the brand new :) GetBrightness method.

WriteableBitmap wb = new WriteableBitmap(myImage, null); 
for(int y = 0 ; y < myImage.Height; y++) 
{ 
   for (int x=0; x < myImage.Width;x++) 
   { 
      byte brightness = wb.GetBrightness(x, y); 
      if(brightness < 20) 
         wb.SetPixel(x,y,Colors.Red); 
   } 
} 
myImagen.Source = wb;

 

And if you don't want to use the newest source code, you can do it with the released 0.9.5.0 like this:

WriteableBitmap wb = new WriteableBitmap(myImage, null); 
const int threshold = 20;
writeableBmp.ForEach((x, y, color) => 
{
   if(color.R < threshold && color.G < threshold && color.B < threshold)
   {
      color = Colors.Red;
   }
   return color;
});

myImagen.Source = wb;

Please use the "Insert Code Snippet" from the toolbar when you post source code.

Aug 4, 2010 at 9:28 AM
Edited Aug 4, 2010 at 10:45 AM
Hello. Thank you for your response, very helpful. I have tried your GetBrightness method, and it does work for JPG images but not PNG. I have tried my own PNGs as well as some random PNGs from the web and in all cases all pixels are converted to red. I'll try to find out whats going on but if you have any ideas I would really appreciate it. One note, the code also works if I try to detect white pixels and convert them to any color. Weird...
Coordinator
Aug 4, 2010 at 10:05 AM

As I already wrote in the first reply it's surely not Black, but transparent, which means that the alpha value (Color.A) is near 0. 

This code will tint the pixels that have an alpha value below the threshold.

WriteableBitmap wb = new WriteableBitmap(myImage, null); 
const int threshold = 20;
writeableBmp.ForEach((x, y, color) => 
{
   if(color.A < threshold)
   {
      color = Colors.Red;
   }
   return color;
});

myImagen.Source = wb;

Aug 4, 2010 at 10:50 AM
Thank you very much, it works fine.
Aug 14, 2010 at 12:36 AM
I just wanted to say although this works I'm getting jagged edges on the images after the color change. Here's what I'm talking about: http://img413.imageshack.us/img413/8565/imgeb.jpg I don't know if anything can be done, I have tried a blur effect but doesn't really look too good.
Coordinator
Aug 14, 2010 at 5:55 AM
This has nothing to do with the WBX. Your black image is anti-aliased. You replace also the gray outlinings with solid green. That's why it's getting aliased. Try a tinting process instead. I described this in my latest Coding4Fun article.
Aug 14, 2010 at 11:11 AM

Thank you, the tint effect works as expected. You're really great !

Nov 13, 2012 at 8:35 AM
Edited Nov 13, 2012 at 8:35 AM

hi teichgraf,

i am new to c# ,to make image black and white i applied tint effect likethis

 

bmp.ForEach((x, y, color) =>
                    {
                       var c = bmp.GetPixel(x, y);
                        byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);
                       // color.A = gray;
                        color.R = gray;
                        color.G = gray;
                        color.B = gray;
                       
                        return color;
                    });

is it right way i am using? and is there any alternate way which could be more efficient?