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

このコードは、oldColor を RGBA ビットマップの新しい色に置き換えます。

新しい色は完全に不透明な白です。古い色は最初のピクセルから取得されます。多くのアイコンとマスク

ストライドは、スキャン ライン/行あたりのバイト数です。

バグ:

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 に答える