2

我正在尝试用一种颜色替换为白色 alpha 来显示图像,以便我可以将其叠加在其他图像之上。我已经有了它,这样我就可以很容易地改变颜色,但是把它变成透明的却让我望而却步。这是我的代码,使用 C# 和 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);
        }

    }

我有两张正在测试的图像。Image1 就像我要处理的那样,原始图像没有透明度。Image2 已经具有透明度。我认为从 image2 (0x00ffffff) 中获取值很容易,但这只会使它变白并掩盖后面的任何图像。

两张图片都是png,格式都是Bgr32。

有谁知道如何使图像透明?

4

1 回答 1

3

使用Bgra32怎么样?

还要确保您了解颜色在内存中的表示方式以及alpha的含义。

于 2009-08-31T14:15:43.440 回答