The GetPixel and SetPixel extension methods are very expensive for multiple iterative changes, since they extract the BitmapContext (the WriteableBitmap's PixelBuffer), make the change, and then writes back the updated PixelBuffer when the call is done with the BitmapContext.
WriteableBitmapEx will share the BitmapContext between multiple calls if you get it first and keep a live reference. This will be significantly faster to read out the PixelBuffer only once, make all of the changes, and then write it back only once.
To do this, use WriteableBitmapEx's BitmapContext object (reachable via the GetBitmapContext extension method) to extract the PixelBuffer then call Get and SetPixel as often as needed on the bitmap context. When done with it, Dispose the BitmapContext to save it back into the WriteableBitmap's PixelBuffer (it'll generally be easier to auto-Dispose the BitmapContext via a using statement).
There is sample code that will give the general idea on the WriteableBitmapEx GitHub at https://github.com/teichgraf/WriteableBitmapEx
Something like:
private void setPixelColors(int xCord, int yCord, int newColor)
{
using (bit.GetBitmapContext())
{
_setPixelColors(xCord, yCord, newColor);
}
}
private void _setPixelColors(int xCord, int yCord, int newColor)
{
Color color = bit.GetPixel(xCord, yCord);
if (color.R <= 5 && color.G <= 5 && color.B <= 5 || newColor == ConvertColorToInt(color))
{
//Debug.WriteLine("The color was black or same returning");
return;
}
setPixelColors(xCord + 1, yCord, newColor);
setPixelColors(xCord, yCord + 1, newColor);
setPixelColors(xCord - 1, yCord, newColor);
setPixelColors(xCord, yCord - 1, newColor);
//Debug.WriteLine("Setting the color here");
bit.SetPixel(xCord, yCord, newColor);
}
This should get the overall speed reasonable, but (as Alex suggests) you should look into non-recursive flood fill algorithms, especially if you have large bitmaps. The recursive algorithm will overflow the stack for large fills. There are a few fairly easy options on Wikipedia: https://en.wikipedia.org/wiki/Flood_fill#Alternative_implementations A simple one is to keep basically the same structure you have here but instead of recursing to handle each new pixel, explicitly handle the stack of to-be-edited pixels. If you know you are targeting small areas then that will probably be fast enough on its own. To support larger ones you may want to optimize further.