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?
你能帮我解释一下这段代码吗? color和oldColor是什么意思?
1 个解决方案
#1
5
This code substitutes and oldColor by a new color in a RGBA bitmap.
此代码用RGBA位图中的新颜色替换oldColor。
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.
Stride是每个扫描行/行的字节数。
Bugs:
错误:
1) bmp.CopyPixels(pixels, stride, 0); only copies the first row. it shouls be bmp.CopyPixels(pixels, stride * bmp.Height, 0);
1)bmp.CopyPixels(像素,步幅,0);只复制第一行。它应该是bmp.CopyPixels(像素,步幅* 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
2)它特定于RGB颜色的特定布局。它不会检查“new BitmapImage”“new int []”和BitmapSource.Create的结果
3) the function's name is wrong.
3)函数的名称错误。
#1
5
This code substitutes and oldColor by a new color in a RGBA bitmap.
此代码用RGBA位图中的新颜色替换oldColor。
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.
Stride是每个扫描行/行的字节数。
Bugs:
错误:
1) bmp.CopyPixels(pixels, stride, 0); only copies the first row. it shouls be bmp.CopyPixels(pixels, stride * bmp.Height, 0);
1)bmp.CopyPixels(像素,步幅,0);只复制第一行。它应该是bmp.CopyPixels(像素,步幅* 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
2)它特定于RGB颜色的特定布局。它不会检查“new BitmapImage”“new int []”和BitmapSource.Create的结果
3) the function's name is wrong.
3)函数的名称错误。