-2

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 Result

lee-m
  • 2,269
  • 17
  • 29
  • 1
    There are many pixels in the background that look black but their value is not (0,0,0) which you compare against. – wohlstad Aug 24 '22 at 05:07
  • @wohlstad you are right, how can I compare the exact color and replce it to my desired color ? – Shaiwal Tripathi Aug 24 '22 at 05:10
  • You can try to change all pixels wihch are close enough to black (e.g. r<5 && g < 5 && b < 5), but it might produce some artifacts on the border. – wohlstad Aug 24 '22 at 05:12
  • 2
    You are not copying the non-"black" pixels to the new image. – emilsteen Aug 24 '22 at 05:59
  • *Never* make posts with "But it's not working". *Always* describe the result you get, and what result you expected. – JonasH Aug 24 '22 at 06:14

1 Answers1

0

The most obvious problem is that you create a new bitmap, but do not copy the color for non black pixels. So you probably should do:

if (actualColor.R == 0 && actualColor.G == 0 && actualColor.B == 0)
       newBitmap.SetPixel(i, j, Color.FromArgb(actualColor.A, Color.Red));
else
    newBitmap.SetPixel(i, j, actualColor);

An alternative would be to update the source bitmap in-place instead of creating a new one.

You might also want to change the color checks to something like

if (0.3 * actualColor.R + 0.59 * actualColor.G + 0.11*actualColor.B < threshold)

To include all pixels with a luminosity lower than some threshold

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • @JohasH I tried your solution but it's dosen't help either. Actually main issue is with this line `if (actualColor.R == 0 && actualColor.G == 0 && actualColor.B == 0)`. – Shaiwal Tripathi Aug 24 '22 at 07:31
  • @ShaiwalTripathi That line looks correct to me, *assuming* the actual color is **completely** black. It might be fine in created images, like a graph in your example, but is typically not appropriate for photos or compressed images. – JonasH Aug 24 '22 at 07:35
  • Actually, if you check my attached image, not every part is pure black. I want to change the color which is pure or close enough to black. – Shaiwal Tripathi Aug 24 '22 at 07:41
  • @ShaiwalTripathi I did check your example image, but the background seem pure black to me, with exception of compression artifacts, that I cannot tell if they are present in the original image, or added by the image host. – JonasH Aug 24 '22 at 07:46
  • @ShaiwalTripathi again, if you posted the actual result instead of "It's not working", we would not have to guess as much as what the actual problem is. – JonasH Aug 24 '22 at 07:48
  • check my update. – Shaiwal Tripathi Aug 24 '22 at 08:00