我試圖增加透明度的HBITMAP對象,但它從來沒有畫任何東西:/HBITMAP增加透明度/ Alpha通道
這是我用來繪製手柄
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);
的代碼,這是應該添加一個alpha通道到HBITMAP
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 for 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 later manipulation
DeleteDC(windowDC);
編輯的代碼:
這是我如何創建位圖 代碼我填的是PixelData取出在後面的一些代碼
m_hBuffer = CreateBitmap(m_ResX, m_ResY, 1, 32, nullptr);
我認爲你必須使用GDI +來設置alpha通道。舊的GDI內容不適用於32位圖像。請參閱:http://stackoverflow.com/questions/3942781/how-to-genrate-a-monochrome-bit-mask-for-a-32bit-bitmap – Mordachai
@Mordachai - 大多數GDI功能都是如此 - 比如BitBlt - 但AlphaBlend(在Win2K中添加)被記錄爲專門支持源圖像中的每像素alpha。 – BrendanMcK
@joeco,你是否嘗試過很多調試來試圖縮小問題的可能性?例如。首先嚐試使用BitBlt,只是爲了確保其餘的代碼和邏輯是好的。也可以嘗試改變阿爾法改變代碼,或許將所有G和B通道設置爲與R通道相同 - 只是爲了確保您的位圖操作代碼是正確的。順便提一句,請注意,AlphaBlend預計源位圖R/G/B將被「預乘」,因此如果您添加了Alpha通道,則可能需要在該階段適當調整R/G/B。 – BrendanMcK