我正在尝试用 alpha 拉伸(stretch) HBITMAP 并将其绘制到花药 hdc。
我使用的是 StretchDIBits,然后是 AlphaBlend,如下面的代码所示。
问题是 AlphaBlend 失败并返回 false。
1. 有谁知道可能是什么原因?
2. 有没有更好的为什么要拉伸(stretch)和绘制透明图像?
void AnimationManager::Draw(HDC hBBDC, Instance sInstance,RECT sClientRect)
{
// sClientRect is the hwnd rect
int nID = GetId(sInstance.nAnemationId);
int nFrameindex = sInstance.nFrameIndexs;
HDC hdcScreen = GetDC(NULL);
HDC hdcMem = CreateCompatibleDC(hdcScreen);
BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;
///******************* create PBITMAPINFO *********************///
GetObject(m_pAnimations[nID]->m_pFramesArray[nFrameindex]->hBmp, sizeof(bmp), &bmp);
cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
if(cClrBits == 1)
cClrBits = 1;
else if(cClrBits <= 4)
cClrBits = 4;
else if(cClrBits <= 8)
cClrBits = 8;
else if(cClrBits <= 24)
cClrBits = 24;
else cClrBits = 32;
if(cClrBits != 24)
pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*(1<<cClrBits));
else
pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
if(cClrBits < 24)
pbmi->bmiHeader.biClrUsed = (1<<cClrBits);
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits + 31)& ~31)/8 * pbmi->bmiHeader.biHeight;
pbmi->bmiHeader.biClrImportant = 0;
///**************************end PBITMAPINFO creation *****************************************///
// I checked the code to this point and it seems to be fine. The problem stasrts here:
///create a pointer to the image bits
LPVOID lpvBits = (LPVOID)LocalAlloc(GPTR,pbmi->bmiHeader.biSizeImage);
GetBitmapBits(m_pAnimations[nID]->m_pFramesArray[nFrameindex]->hBmp, pbmi->bmiHeader.biSizeImage, lpvBits);
///stretch
bool test = StretchDIBits(hdcMem,0,sClientRect.bottom,sClientRect.right, -1*sClientRect.bottom,0,0,pbmi->bmiHeader.biWidth,pbmi->bmiHeader.biHeight, lpvBits,pbmi,DIB_RGB_COLORS, SRCCOPY);
///blend
test = AlphaBlend(hBBDC,0,0,sClientRect.right, sClientRect.bottom,hdcMem,0,0,sClientRect.right,sClientRect.bottom,m_sBlendfunc);
test = DeleteDC(hdcMem); // after CreateCompatibleDC
test = ReleaseDC(NULL, hdcScreen); // after GetDC
LocalFree(pbmi);
LocalFree(lpvBits);
}
最佳答案
如果 StretchDIBits
也可以拉伸(stretch),为什么要使用 AlphaBlend
?
关于c - AlphaBlend 返回 'false' 的原因可能是什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4267784/