質問

私は他の画像の上に、それをレイヤーすることができるように白いアルファに置き換え色のいずれかで表示する画像を取得しようとしています。私は簡単に十分な色を変更できるように、それを得たが、それは私を逃亡されて透明に変更しました。ここに私のコードは、C#やWPFを使用して、です。

    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);
        }

    }

私は私がこれをテストしています2つのイメージを持っています。画像1は、私は、元の画像には、透明性に取り組んでいないことするつもりです何のようなものです。画像2は、すでに透明性を有します。私はそれだけで画像2(0x00ffffff)から値を取得するのは簡単だろうと思ったが、それはちょうどそれが白になり、背後にある任意の画像をカバーします。

両方の画像はPNGです、そして両方の形式はBgr32である。

誰もが透明であるように画像を取得する方法を知っていますか?

役に立ちましたか?

解決

どの程度使用して Bgra32

また、あなたは色がメモリとどのようなアルファの手段でどのように表現されるかを理解しておいてください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top