Search code examples
c#imageimage-processingpixel

Making black pixels transparent


I have two images and they have the same size. Now I need to remove pixels in Image 1 which is color black in btmp

for (int c = 0; c < selFrame.Width; c++)
  for (int j = 0; j < selFrame.Height; j++)
    {

     var pixel = selFrame.GetPixel(c, j);
     var pixel2 = btmp.GetPixel(c, j);
     if (pixel2.Equals(Color.Black) || pixel2.IsEmpty)
       {
       MessageBox.Show("qwe");
       selFrame.SetPixel(c, j, Color.Transparent);
       }

    }

MessageBox doesn't show, so that means that it didn't go through the If condition.

Here is the btmp

Image 2


Solution

  • Because [255,0,0,0] does not equal Color.Black.

    From the documentation:

    For example, Black and FromArgb(0,0,0) are not considered equal, since Black is a named color and FromArgb(0,0,0) is not.

    As per the advice at the above documentation, change your check to be :

    if (pixel2.ToArgb() == Color.Black.ToArgb() || pixel2.IsEmpty)