I am trying to replace black pixels with red in an image. Here is my image My_Image
Here is my code
public static Bitmap ChangeColor(Bitmap scrBitmap)
{
Color newColor = Color.Red;
Color actualColor;
Bitmap newBitmap = new Bitmap(scrBitmap.Width, scrBitmap.Height);
for (int i = 0; i < scrBitmap.Width; i++)
{
for (int j = 0; j < scrBitmap.Height; j++)
{
actualColor = scrBitmap.GetPixel(i, j);
if (actualColor.R == 0 && actualColor.G == 0 && actualColor.B == 0)
newBitmap.SetPixel(i, j, Color.FromArgb(actualColor.A, Color.Red));
}
}
return newBitmap;
}
On Load
Bitmap image = new Bitmap(@"D:\test.jpg");
pictureBox1.Image = ChangeColor(image);
I just want to change the color of the black background to red but it's not working. What am I missing ?
Edit: I tried the solution of This Post but it changes the color of the whole image.
Edit2: I tried @JonasH solution
if (0.3 * actualColor.R + 0.59 * actualColor.G + 0.11 * actualColor.B < 10)
newBitmap.SetPixel(i, j, Color.FromArgb(actualColor.A, Color.Red));
else
newBitmap.SetPixel(i, j, actualColor);
It worked but not perfectly. Some pixels which are not pure black but close enough to black are still not changed. Here is the