2

First of all Ill explain what I try to do, just in the case that someone comes up with a better approach I need to blend too images using the "color" stile from photoshop (you know, the blending methods: Screen, hard light, color ... )

So, I have my base image (a png) and WriteableBitmap generated on execution time (lets call it color mask). Then I need to blend this two images using the "color" method and show the result in a UI component.

So far what Im trying is just to draw things on the WriteableBitmap but I'm facing unexpected behavior with the alpha channel.

My code so far:

// variables declaration
WriteableBitmap img = new  WriteableBitmap(width, height, 96,96,PixelFormats.Bgra32,null); 
pixels = new uint[width * height];

//function for setting the color of one pixel
private void SetPixel(int x, int y, Color c)
    {
        int pixel = width * y + x;

        int red = c.R;
        int green = c.G;
        int blue = c.B;
        int alpha = c.A;

        pixels[pixel] = (uint)((blue << 24) + (green << 16) + (red << 8) + alpha);

    }

//function for paint all the pixels of the image
private void Render()
    {
        Color c = new Color();
        c.R = 255; c.G = 255; c.B = 255; c.A = 50;
        for (int y = 0; y < height; y++)
            for (int x = 0; x < width; x++)
                SetPixel(x, y, c);



        img.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * 4, 0);
        image1.Source = img;  // image1 is a WPF Image in my XAML
    }

Whenever I run the code with the color c.A = 255 I get the expected results. The whole Image is set to the desired color. But if I set c.A to a different value I get weird things. If I set the color to BRGA = 0,0,255,50 I get a dark blue almost black. If I set it to BRGA = 255,255,255,50 I get a yellow ...

Any clue !?!?!

Thanks in advance !

javirs
  • 1,049
  • 26
  • 52

1 Answers1

2

Change the order of your colour components to

pixels[pixel] = (uint)((alpha << 24) + (red << 16) + (green << 8) + blue);
Andy
  • 6,366
  • 1
  • 32
  • 37
  • Totally understandable when they write the colour components the wrong way round on the enum. – Andy Dec 11 '12 at 09:15