Frage

Ich versuche, ein Bild anzuzeigen, um mit einer der mit einem weißen Alpha ersetzt Farben, so dass ich es auf anderen Bildern Schicht kann. Ich habe es so bekam, dass ich die Farben leicht genug ändern, aber ändert es transparent sein wird, entzieht mir. Hier ist mein Code, C # und 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);
        }

    }

Ich habe zwei Bilder bekam, dass ich testen diese mit. Image1 ist wie das, was ich werde zu arbeiten, keine Transparenz im Originalbild. Image2 hat bereits Transparenz. Ich dachte, es einfach wäre, nur den Wert greifen von image2 (0x00FFFFFF), aber das macht es nur weiß und deckt sich jede Bilder hinter sich.

Beide Bilder sind png und das Format für beide ist Bgr32.

Wer weiß, wie das Bild zu bekommen transparent sein?

War es hilfreich?

Lösung

Wie wäre es mit Bgra32 ?

Auch stellen Sie sicher, Sie verstehen, wie die Farbe im Speicher dargestellt wird und was alpha Mittel.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top