将Alpha通道添加到位图或将位图转换为32位

时间:2013-08-25 17:49:10

标签: c++ winapi

解决: 当前代码示例:

HBITMAP TwoBitmap(HDC &hdc, RECT &rc)
{
    HDC hdcmem = CreateCompatibleDC(hdc);
    HBITMAP hbitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left, rc.bottom-rc.top);
    SelectObject(hdcmem, hbitmap);

    HBRUSH hBrush = GetSysColorBrush(2);
    FillRect(hdcmem, &rc, hBrush);
    DeleteObject((HBRUSH)hBrush);

    DeleteDC(hdcmem);

    return hbitmap;
}

void CreateTransparentBitmap(HBITMAP &hBitmap)
{
    BITMAP TemporaryBitmap;
    GetObject(hBitmap, sizeof(BITMAP), &TemporaryBitmap);

    HDC MemoryDC = CreateCompatibleDC(NULL);

    BITMAPINFO BitmapInfo;
    ZeroMemory(&BitmapInfo,sizeof(BITMAPINFO));
    BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    BitmapInfo.bmiHeader.biWidth = TemporaryBitmap.bmWidth;
    BitmapInfo.bmiHeader.biHeight = TemporaryBitmap.bmHeight;
    BitmapInfo.bmiHeader.biPlanes = 1;
    BitmapInfo.bmiHeader.biBitCount = 32;
    BitmapInfo.bmiHeader.biCompression = BI_RGB;

    RGBQUAD* pArgb = NULL;
    HBITMAP hTemporaryBitmap = CreateDIBSection(MemoryDC, &BitmapInfo, DIB_RGB_COLORS, (void**)&pArgb, NULL, 0);

    GetDIBits(MemoryDC, hBitmap, 0, BitmapInfo.bmiHeader.biHeight, pArgb, &BitmapInfo, DIB_RGB_COLORS);

    for (int i = 0; i < BitmapInfo.bmiHeader.biWidth * BitmapInfo.bmiHeader.biHeight; i++)
        pArgb[i].rgbReserved = 255;

    DeleteDC(MemoryDC);

    DeleteObject(hBitmap);
    hBitmap = hTemporaryBitmap;
}

case WM_PAINT:
    {
        PAINTSTRUCT p;
        HDC hDC = BeginPaint(hWnd, &p);
        HDC hdcmem = CreateCompatibleDC(hDC);

        HBITMAP hImage = TwoBitmap(hDC, p.rcPaint);

        CreateTransparentBitmap(hImage);

        SelectObject(hdcmem, hImage);

        BLENDFUNCTION bfn;
        bfn.BlendOp = AC_SRC_OVER;
        bfn.BlendFlags = 0;
        bfn.SourceConstantAlpha = 10;
        bfn.AlphaFormat = AC_SRC_ALPHA;

        AlphaBlend(hDC, 0, 0, 100, 64, hdcmem, 0, 0, 100, 64, bfn);

        bfn.SourceConstantAlpha = 80;
        AlphaBlend(hDC, 200, 0, 100, 64, hdcmem, 0, 0, 100, 64, bfn);

        bfn.SourceConstantAlpha = 255;
        AlphaBlend(hDC, 400, 0, 100, 64, hdcmem, 0, 0, 100, 64, bfn);

        BitBlt(hDC, 0, 100, 100, 64, hdcmem, 0, 0, SRCCOPY);

        DeleteDC(hdcmem);

        DeleteObject(hImage);

        EndPaint(hWnd, &p);
    }
    break;

对于这个例子,我有一个使用CreateCompatibleBitmap()创建的简单位图:

HBITMAP TwoBitmap(HDC &hdc, RECT &rc)
{
    HDC hdcmem = CreateCompatibleDC(hdc);
    HBITMAP hbitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left, 
                                             rc.bottom-rc.top);
    SelectObject(hdcmem, hbitmap);

    HBRUSH hBrush = GetSysColorBrush(3);
    FillRect(hdcmem, &rc, hBrush);
    DeleteObject((HBRUSH)hBrush);

    DeleteDC(hdcmem);

    return hbitmap;
}

现在,我想要实现的是将alpha通道添加到此位图(如果它还没有)。使用下面的代码,我尝试创建一个32位位图,然后将旧位图复制到这个新位图中。最后,我使用AlphaBlend()和BitBlt()函数将新位图打印到屏幕上,并且像往常一样,我获得单色alpha混合图像和完美的BitBlt图像。

所以我的问题出现了 - 我错过了什么,做错了什么?我在这里很丢失。我的代码(不介意GDI泄漏):

编辑:

感谢 PhoenixX_2 我能解开这个谜团,我只需要将Alpha位设置为255.以下是我的代码,请记住它不是无泄漏的。

PAINTSTRUCT p;
HDC hDC = BeginPaint(hWnd, &p);

HDC BufferDC = CreateCompatibleDC(NULL);

BITMAPINFO BufferInfo;
ZeroMemory(&BufferInfo,sizeof(BITMAPINFO));
BufferInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BufferInfo.bmiHeader.biWidth = p.rcPaint.right-p.rcPaint.left;
BufferInfo.bmiHeader.biHeight = p.rcPaint.bottom-p.rcPaint.top;
BufferInfo.bmiHeader.biPlanes = 1;
BufferInfo.bmiHeader.biBitCount = 32;
BufferInfo.bmiHeader.biCompression = BI_RGB;
RGBQUAD* pArgb;
HBITMAP BufferBitmap = CreateDIBSection(BufferDC, &BufferInfo, DIB_RGB_COLORS,
                                       (void**)&pArgb, NULL, 0);

HBITMAP hImage  =  TwoBitmap(hDC, p.rcPaint);

GetDIBits(BufferDC, hImage, 0, BufferInfo.bmiHeader.biHeight, pArgb, 
          &BufferInfo, DIB_RGB_COLORS);

//----------------------------------the part I was missing
for (int i = 0; i < BufferInfo.bmiHeader.biWidth 
     * BufferInfo.bmiHeader.biHeight; i++)
pArgb[i].rgbReserved = 255;
//----------------------------------

SelectObject(BufferDC, BufferBitmap);

BLENDFUNCTION bfn;
bfn.BlendOp = AC_SRC_OVER;
bfn.BlendFlags = 0;
bfn.SourceConstantAlpha = 10;
bfn.AlphaFormat = AC_SRC_ALPHA;

AlphaBlend(hDC, 0, 0, 100, 64, BufferDC, 0, 0, 100, 64, bfn);

bfn.SourceConstantAlpha = 80;
AlphaBlend(hDC, 200, 0, 100, 64, BufferDC, 0, 0, 100, 64, bfn);

bfn.SourceConstantAlpha = 255;
AlphaBlend(hDC, 400, 0, 100, 64, BufferDC, 0, 0, 100, 64, bfn);

BitBlt(hDC, 0, 100, 100, 64, BufferDC, 0, 0, SRCCOPY);

DeleteDC(BufferDC);

DeleteObject((HBITMAP)BufferBitmap);

EndPaint(hWnd, &p);

1 个答案:

答案 0 :(得分:2)

我真的建议你使用像FreeImage这样的库。除了相当轻量级和非常开放的许可证之外,该库的好处在于它可以轻松处理所有图像类型。然后从任何格式(包括不同的位图格式)转换为32位是微不足道的。渲染和其他任务也是如此。

除此之外,如果您对使用库不感兴趣,只需自己操作像素数据即可。从24位转换到32位是相当微不足道的两点和铸造。这样的事情应该做:

uint8_t* pArgb;
uint8_t* pRgb;

GetDIBits(BufferDC, hImage, 0, BufferInfo.bmiHeader.biHeight, pRgb, &BufferInfo, DIB_RGB_COLORS);

pArgb = (uint8_t*)malloc(4 * BufferInfo.bmiHeader.biWidth * BufferInfo.bmiHeader.biHeight);
for (int i = 0; i < BufferInfo.bmiHeader.biWidth * BufferInfo.bmiHeader.biHeight; ++i) {
    *(int*)pArgb = 0xff000000 | (*(int*)(pRgb) >> 8);
    pArgb += 4;
    pRgb += 3;
}