首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WriteableBitmapEx alpha混合颜色问题

WriteableBitmapEx alpha混合颜色问题
EN

Stack Overflow用户
提问于 2019-04-16 23:00:25
回答 1查看 122关注 0票数 0

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

我是不是错误地使用了WriteableBitmapEx,误解了它是如何工作的,还是它是一个错误?

这是在.Net 4.6.1和WriteableBitmapEx 1.6.2上。

代码语言:javascript
复制
    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;
    }

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-17 14:08:43

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

您可以简单地使用ConvertColor method that comes with WBX或滚动您自己的,如下所示:

代码语言:javascript
复制
    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;
     }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55711208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档