문제

나는 다른 이미지 위에 겹칠 수 있도록 흰색 알파로 대체 된 색상 중 하나로 이미지를 표시하려고합니다. 색상을 충분히 쉽게 바꿀 수 있도록 가져 왔지만 투명하게 변경하면 나를 피할 수 있습니다. 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);
        }

    }

이것을 테스트하는 두 개의 이미지가 있습니다. Image1은 내가 작업 할 작업과 같습니다. 원본 이미지의 투명성이 없습니다. Image2는 이미 투명성을 가지고 있습니다. 나는 image2 (0x00ffffff)에서 값을 잡는 것이 쉽다고 생각했지만 그냥 흰색으로 만들고 뒤에 이미지를 덮습니다.

두 이미지 모두 PNG이고 두 이미지 모두 BGR32입니다.

이미지를 투명하게 만드는 방법을 아는 사람이 있습니까?

도움이 되었습니까?

해결책

사용하는 것은 어떻습니까 BGRA32?

또한 색상이 메모리에 어떻게 표현되는지 이해하고 알파 수단.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top