WPF WriteableBitmap 透明度问题

标签 wpf writeablebitmap

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

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

到目前为止,我尝试的只是在 WriteableBitmap 上绘制内容,但我面临着 alpha channel 的意外行为。

到目前为止我的代码:

// 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 我会得到黄色...

有什么线索吗!?!?!

提前致谢!

最佳答案

将颜色分量的顺序更改为

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

关于WPF WriteableBitmap 透明度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13816487/

相关文章:

c# - 如何在 WPF 中为图表添加水平线 ("goal line")?

c# - 计算 WriteableBitmap.WritePixels 方法所需的缓冲区大小

c# - Sobel 运算符和与 WriteableBitmapEx 的卷积

c# - 使用 WPF 将 WriteableBitmap 保存到文件

c# - 如何在 Windows Phone 上合并 2 个图像

c# - 值不在 WriteableBitmap 异常范围内

wpf - 在控件中对齐文本底部

c# - 如何在 MVVM 中单击时清除文本框

c# - WPF 中的 MVVM 模式 - 如何处理我无法控制的模型?

c# - 在更改的回调中设置 DependencyProperty 值