1
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

4

1 回答 1

5

此代码用 RGBA 位图中的新颜色替换和 oldColor。

新颜色是完整的 - 完全不透明的白色。旧颜色取自第一个像素。许多图标和面具都可以

步幅是每条扫描线/行有多少字节。

错误:

1)bmp.CopyPixels(像素,步幅,0);只复制第一行。它应该是 bmp.CopyPixels(pixels, stride * bmp.Height, 0);

2) 它假设 RGB 颜色的特定布局。它不检查“new BitmapImage”“new int[]”和 BitmapSource.Create 的结果

3) 函数名错误。

于 2010-08-25T19:37:36.630 回答