Untitled
Untitled

Reputation: 113

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

Upvotes: 0

Views: 156

Answers (1)

Rob
Rob

Reputation: 27367

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)

Upvotes: 1

Related Questions