0

我正在尝试将 WriteableBitmap 与 WriteableBitmapEx 扩展方法一起使用。下面是代码示例以及两个屏幕截图。第一个屏幕截图是我的代码生成的,第二个屏幕截图是我所期望的(通过 Paint.Net 手绘)。

我是否错误地使用了 WriteableBitmapEx,误解了它的工作原理还是一个错误?

这是在 .Net 4.6.1 和 WriteableBitmapEx 1.6.2 上。

    public MainWindow()
    {
        InitializeComponent();

        _bitmap = BitmapFactory.New(1000, 1000);

        _bitmap.FillRectangle(0,0,1000,1000, ColorToInt(Colors.White), true);

        _bitmap.FillRectangle(100, 100, 500, 500, ColorToInt(_color), true);
        _bitmap.FillRectangle(150, 150, 550, 550, ColorToInt(_color), true);
        _bitmap.FillRectangle(200, 200, 600, 600, ColorToInt(_color), true);
        _bitmap.FillRectangle(250, 250, 650, 650, ColorToInt(_color), true);
        _bitmap.FillRectangle(300, 300, 700, 700, ColorToInt(_color), true);
        _bitmap.FillRectangle(350, 350, 750, 750, ColorToInt(_color), true);
        _bitmap.FillRectangle(400, 400, 800, 800, ColorToInt(_color), true);

        Image1.Source = _bitmap;
    }

    private readonly Color _color = Color.FromArgb(25, 0, 0, 255);
    private WriteableBitmap _bitmap;

    private int ColorToInt(Color c)
    {
        return c.A << 24 | c.R << 16 | c.G << 8 | c.B;
    }

我的代码生成的图像。 蓝道逐渐失传?

Paint.Net 生成我所期望的

4

1 回答 1

1

WriteableBitmap 在内部使用预乘 alpha,因此当您转换颜色时,您必须考虑到这一点并将颜色通道与 alpha 相乘。

您可以简单地使用WBX 附带的 ConvertColor 方法,或者像这样滚动您自己的方法:

    public static int ConvertColor(Color color)
    {
        var col = 0;

        if (color.A != 0)
        {
            var a = color.A + 1;
            col = (color.A << 24)
              | ((byte)((color.R * a) >> 8) << 16)
              | ((byte)((color.G * a) >> 8) << 8)
              | ((byte)((color.B * a) >> 8));
        }

        return col;
     }
于 2019-04-17T06:08:43.040 回答