1 .net c# wpf bitmapimage
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);
}
}
Run Code Online (Sandbox Code Playgroud)
你能帮我解释一下这段代码吗?是什么color和oldColor意味着什么?
此代码用RGBA位图中的新颜色替换oldColor.
新的颜色是完整的 - 不透明的白色.旧颜色取自第一个像素.许多图标和面具
Stride是每个扫描行/行的字节数.
错误:
1)bmp.CopyPixels(像素,步幅,0); 只复制第一行.它应该是bmp.CopyPixels(像素,步幅*bmp.Height,0);
2)它特定于RGB颜色的特定布局.它不会检查"new BitmapImage""new int []"和BitmapSource.Create的结果
3)函数的名称错误.