【问题标题】:WPF WriteableBitmap problems with transparencyWPF WriteableBitmap 透明度问题
【发布时间】:2012-12-11 08:42:23
【问题描述】:

首先我会解释我试图做什么,以防有人想出更好的方法 我需要使用 Photoshop 中的“颜色”框来混合图像(你知道,混合方法:屏幕、强光、颜色......)

所以,我有我的基本图像(一个 png)和在执行时生成的 WriteableBitmap(我们称之为颜色掩码)。然后我需要使用“颜色”方法混合这两个图像,并将结果显示在 UI 组件中。

到目前为止,我尝试的只是在 WriteableBitmap 上绘制东西,但我正面临 alpha 通道的意外行为。

到目前为止我的代码:

// 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
    }

每当我运行颜色为 c.A = 255 的代码时,我都会得到预期的结果。整个图像设置为所需的颜色。但是如果我将 c.A 设置为不同的值,我会得到奇怪的东西。 如果我将颜色设置为 BRGA = 0,0,255,50,我会得到一个几乎是黑色的深蓝色。如果我将它设置为 BRGA = 255,255,255,50 我会得到一个黄色...

任何线索!?!?!

提前致谢!

【问题讨论】:

    标签: wpf writeablebitmap


    【解决方案1】:

    将颜色组件的顺序更改为

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

    【讨论】:

    • 当他们在枚举上以错误的方式编写颜色分量时,完全可以理解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 2013-01-24
    • 2013-06-21
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多