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);
}
}
你能为我解释这段代码吗? color
和oldColor
的含义是什么?
答案 0 :(得分:5)
此代码用RGBA位图中的新颜色替换oldColor。
新颜色已满 - 白色不透明。旧颜色取自第一个像素。许多图标和面具都
Stride是每个扫描行/行的字节数。
错误:
1)bmp.CopyPixels(像素,步幅,0);只复制第一行。它应该是bmp.CopyPixels(像素,步幅* bmp.Height,0);
2)它讨论RGB颜色的特定布局。它不会检查“new BitmapImage”“new int []”和BitmapSource.Create
的结果3)函数名称错误。