Support Center

Using AlphaBlend to draw Transparent 32 bit images with ImageMan DLL Suite V 8.x

Last Updated: May 25, 2011 05:19PM EDT

 

The following code uses the Win 32 API function AlphaBlend to draw a 32 bit image to a DC using the Alpha channel data for transparency. Note that if the image is not 32 bits then the AlphaBlend function will fail and return FALSE.
 
HANDLE hImage = ImgOpenSolo("d:\\t32bit.png", NULL);
 
ImgLoad( hImage, NULL);

HANDLE hBitmap = ImgGetHBitmap( hImage );
HDC ImgDC = ::CreateCompatibleDC( NULL );
HANDLE hObj = ::SelectObject( ImgDC, hBitmap );
 
 
int flags;
LPBITMAPINFO lpInfo = ImgGetInfo(hImage, &flags);
 
BLENDFUNCTION bf;
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 0xff;
bf.AlphaFormat = AC_SRC_ALPHA;

if(lpInfo->bmiHeader.biBitCount == 32 ) // Alpha Channel
{
LPVOID lpBits = ImgGetDataPtr(hImage);
 
// pre-multiply rgb channels with alpha channel - AlphaBlend requires this to be done, only needs to be done once of course
for (int y=0; y<lpInfo->bmiHeader.biHeight; ++y)
{
BYTE *pPixel= (BYTE *) lpBits +lpInfo->bmiHeader.biWidth * 4 * y;
for (int x=0; x<lpInfo->bmiHeader.biWidth ; ++x)
{
pPixel[0]= pPixel[0]*pPixel[3]/255;
pPixel[1]= pPixel[1]*pPixel[3]/255;
pPixel[2]= pPixel[2]*pPixel[3]/255;
pPixel+= 4;
}
}
}
int xPos = 200;

int yPos = 200;
 
bool bDrawn = AlphaBlend(pDC->m_hDC, xPos, yPos, lpInfo->bmiHeader.biWidth, lpInfo->bmiHeader.biHeight,  ImgDC, 0, 0, lpInfo->bmiHeader.biWidth, lpInfo->bmiHeader.biHeight, bf  );
 
::SelectObject(ImgDC, hObj);
 
::ReleaseDC(NULL, ImgDC);

Contact Us