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);
}
}
Could you explain this code for me? What does color
and oldColor
mean?
This code substitutes and oldColor by a new color in a RGBA bitmap.
The new color is full - copmletely opaque white. The old color is taken from the first pixel. many icons and masks do
Stride is how many bytes per scan line / row there are.
Bugs:
1) bmp.CopyPixels(pixels, stride, 0); only copies the first row. it shouls be bmp.CopyPixels(pixels, stride * bmp.Height, 0);
2) It assusmes a particualr layout of the RGB colors. It allo does not check the results of "new BitmapImage" "new int[]" and BitmapSource.Create
3) the function's name is wrong.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.