bloodless2010
bloodless2010

Reputation: 349

Image isn't being transparent

so I'm working on this program, and for some reason pink wont go transparent, here's my code:

if (chatIcons.GetPixel(x, y) == Color.FromArgb(255, 0, 255) || chatIcons.GetPixel(x, y) == Color.FromArgb(0, 255, 255))
{
chatIcons.SetPixel(x, y, Color.FromArgb(0, 0, 0, 0));
}

0, 255, 255 is the cyan (works) 255, 0, 255 is the pink (doesn't work) Why is that? the code works on one bit but not the other.

Oh and here's my image: enter image description here

Upvotes: 0

Views: 112

Answers (3)

terrybozzio
terrybozzio

Reputation: 4542

If its bitmap(and if your image supports Alpha) you can try this:

chatIcons = ChangeColor(chatIcons,(byte)255,(byte)0,(byte)255);

public static Bitmap ChangeColor(Bitmap sourceBitmap, byte blue, byte green, byte red)
{
       BitmapData sourceData = sourceBitmap.LockBits(new Rectangle(0, 0,
                                    sourceBitmap.Width, sourceBitmap.Height),
                                    ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

       byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height];

       Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);

       sourceBitmap.UnlockBits(sourceData);

       for (int k = 0; k + 4 < pixelBuffer.Length; k += 4)
       {
           if (pixelBuffer[k] == blue && pixelBuffer[k + 1] == green && pixelBuffer[k + 2] == red)
           {
                pixelBuffer[k] = 0;
                pixelBuffer[k + 1] = 0;
                pixelBuffer[k + 2] = 0;
                pixelBuffer[k + 3] = 0;
           }
        }

        Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);

        BitmapData resultData = resultBitmap.LockBits(new Rectangle(0, 0,
                                    resultBitmap.Width, resultBitmap.Height),
                                    ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

         Marshal.Copy(pixelBuffer, 0, resultData.Scan0, pixelBuffer.Length);
            resultBitmap.UnlockBits(resultData);

         return resultBitmap;
}

Upvotes: 1

Gayot Fow
Gayot Fow

Reputation: 8792

I opened the image with Axialis and was presented with this result (no modifications, but zoomed for capture)...

zoomed

So this would lead to the conclusion that the encoding inside the image is such that the pink pixels are interpreted as transparent by SOME decoders and not others. Photoshop decoded and presented the image as expected. You may need to open and save it under Photoshop to 'override' whatever encoding is affecting the pink pixels.

Also about your code for colour detection. A very deep zoom with Photoshop revealed lots of artefacts to the extent that an exact detection method such as yours is likely to fail in about 10 - 20 % of the pixels. You might consider a detection method along the lines of 'IsNearlyPink'

Photoshop zoom below...

enter image description here

Upvotes: 1

0xDEADBEEF
0xDEADBEEF

Reputation: 3431

The PixelFormat of your Bitmap-Object have to support the Alpha-Channel, like PixelFormats.Bgra32

Upvotes: 0

Related Questions