tags:

views:

272

answers:

2

I'm trying to get an image to display with one of the colors replaced with a white alpha so that I can layer it on top of other images. I've got it so that I can change colors easily enough, but changing it to be transparent is eluding me. Here's my code, using C# and WPF.

    private void SetAlpha(string location)
    {
        //bmp is a bitmap source that I load from an image
        bmp = new BitmapImage(new Uri(location));
        int[] pixels = new int[(int)bmp.Width * (int)bmp.Height];
        //still not sure what 'stride' is.  Got this part from a tutorial
        int stride = (bmp.PixelWidth * bmp.Format.BitsPerPixel + 7)/8;

        bmp.CopyPixels(pixels, stride, 0);
        int oldColor = pixels[0];
        int red = 255;
        int green = 255;
        int blue = 255;
        int alpha = 0;
        int color = (alpha << 24) + (red << 16) + (green << 8) + blue;

        for (int i = 0; i < (int)bmp.Width * (int)bmp.Height; i++)
        {
            if (pixels[i] == oldColor)
            {
                pixels[i] = color;
            }
        }
            //remake the bitmap source with these pixels
            bmp = BitmapSource.Create(bmp.PixelWidth, bmp.PixelHeight, bmp.DpiX, bmp.DpiY, bmp.Format, bmp.Palette, pixels, stride);
        }

    }

I've got two images that I'm testing this with. Image1 is like what I am going to be working on, no transparency in the original image. Image2 already has transparency. I thought it would be easy to just grab the value from image2 (0x00ffffff) but that just makes it white and covers up any images behind.

Both images are png, and the format for both is Bgr32.

Does anyone know how to get the image to be transparent?

+2  A: 

How about using Bgra32?

Also make sure you understand how the color is represented in memory and what alpha means.

Thanks!Changing bmp = BitmapSource.Create(bmp.PixelWidth, bmp.PixelHeight, bmp.DpiX, bmp.DpiY, bmp.Format, bmp.Palette, pixels, stride);to bmp = BitmapSource.Create(bmp.PixelWidth, bmp.PixelHeight, bmp.DpiX, bmp.DpiY, bmp.Format, bmp.Palette, pixels, stride);fixed it.Is there anything that I'm doing with the colors that are making you nervous?
Califer
No... it's pretty much ok. I was just trying to make sure you understand what you are doing, not only taking stuff from a tutorial :)
Oops, I didn't actually post the fix :P The second bmp.Format should be 'System.Windows.Media.PixelFormats.Bgra32' instead.
Califer
A: 

hi please can you help me - i need to detect transparent pixels - thanks in advance.

in4man
Hi in4man. I don't mind helping, but I don't have very much to go off of. In general, images come with Red, Green, Blue and Alpha. Alpha is what determines if each pixel is transparent or not. However, since you don't even mention what language you're using of what the image is being stored in I can't be of more help.You should probably make your own question rather than just piggy-back on someone else's question. This makes it easier for people who want to help to find you. Good luck!
Califer
yeah thanks i tri
in4man
How can i use this for transparent dots detection. thanks in.
in4man