HBITMAP 添加透明度/Alpha 通道

问题描述 投票:0回答:1

我正在尝试向 hbitmap 对象添加透明度,但它从不绘制任何内容:/

这是我用来绘制手柄的代码:

HDC hdcMem = CreateCompatibleDC(hDC);
    HBITMAP hbmOld = (HBITMAP) SelectObject(hdcMem, m_hBitmap);
    
    BLENDFUNCTION blender = {AC_SRC_OVER, 0, (int) (2.55 * 100), AC_SRC_ALPHA}; // blend function combines opacity and pixel based transparency
    AlphaBlend(hDC, x, y, rect.right - rect.left, rect.bottom - rect.top, hdcMem, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, blender);
    
    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);

这是应该向 hbitmap 添加 alpha 通道的代码

BITMAPINFOHEADER bminfoheader;
    ::ZeroMemory(&bminfoheader, sizeof(BITMAPINFOHEADER));
    bminfoheader.biSize        = sizeof(BITMAPINFOHEADER);
    bminfoheader.biWidth       = m_ResX;
    bminfoheader.biHeight      = m_ResY;
    bminfoheader.biPlanes      = 1;
    bminfoheader.biBitCount    = 32;
    bminfoheader.biCompression = BI_RGB;

    HDC windowDC = CreateCompatibleDC(0);
    unsigned char* pPixels = new unsigned char[m_ResX * m_ResY * 4];

    GetDIBits(windowDC, m_hBitmap, 0, m_ResY, pPixels, (BITMAPINFO*) &bminfoheader, DIB_RGB_COLORS); // load pixel info
    
    // add alpha channel values of 255 to every pixel if bmp
    for (int count = 0; count < m_ResX * m_ResY; count++)
    {
        pPixels[count * 4 + 3] = 255; <----  here i've tried to change the value to test different transparency, but it doesn't change anything
    }
    SetDIBits(windowDC, m_hBitmap, 0, GetHeight(), pPixels, (BITMAPINFO*) &bminfoheader, DIB_RGB_COLORS); // save the pixel info for manipulation later
    
    DeleteDC(windowDC);

编辑:

这是我创建位图的代码 我稍后在一些代码中填充像素数据

m_hBuffer = CreateBitmap( m_ResX, m_ResY, 1, 32, nullptr );
c++ winapi gdi hbitmap
1个回答
3
投票

这很有趣!

猜猜这会打印出什么?

#include <stdio.h>

int main()
{
    printf("%d\n", (int) (2.55 * 100));
    return 0;
}

答案:254 - 不是 255。这里发生了两件事:

  • 浮点数通常是不精确的 - 2.55 不能用精确表示 2.55 的二进制值表示 - 它可能类似于 2.5499963...(我只是编造出来的,但你明白了 - 本质上数字表示为2 的分数之和 - 因为二进制以 2 为基数 - 所以 0.5 或 0.25 之类的数字可能可以精确表示,但大多数其他数字将表示为近似值,因为浮点打印例程会注意到这一点。转换回基数 10 进行显示,这本质上引入了另一种不精确性,最终抵消了第一个:因此您看到的分配值或打印输出值并不完全是存储在内存中的表示值。

  • 转换为 int 截断 - 即向下舍入。

将这些放在一起,你的 (2.55 * 100) 会得到 254,而不是 255 - 并且你必须拥有神奇值 255 才能使每像素 alpha 工作。

所以这里的教训是坚持纯整数。或者,如果您确实需要从浮点转换为整数,请注意发生了什么,并解决它(例如,添加 0.5,然后截断 - 或使用类似的技术。)

顺便说一句,这是一种逐行执行代码并检查每一步的所有输入和输出(调试时永远不能太偏执!)可能已经显示出问题的情况之一;在进入 AlphaBlend 之前,当您将鼠标悬停在该参数上时(假设使用 DevStudio 或类似编辑器),您应该会看到 254,并意识到出现了问题。

© www.soinside.com 2019 - 2024. All rights reserved.