题
我试图获得与具有白色的α取代的一种颜色来显示,这样我可以在其它图像的顶部层它的图像。我知道了,这样我可以改变颜色很轻松了,但改变它是透明的躲避我。这里是我的代码,用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);
}
}
我已经得到了我与测试此两个图像。此搜索是像什么,我将要工作的,在原始图像中没有透明度。镜像2已经有透明度。我认为这将是很容易只是抓住从图像2(至0x00FFFFFF)的值,但只是让IT白领和背后掩盖的任何图像。
两个图像都PNG和两者的格式是Bgr32。
有谁知道如何获得图像是透明的?
不隶属于 StackOverflow