Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Any one have the example for Alpha Blended bitmap image drawing?


Thanks in advance.
Posted
Updated 27-May-13 3:58am
v3

1 solution

To draw a bitmap that is provided as DIB (Device Independant Bitmap), it must be converted to a device (usually: screen) compatible DDB (Device Dependant Bitmap). This can be done using the CreateDIBitmap()[^] GDI function:
C++
// LPCVOID lpData points to a DIB
LPBITMAPINFOHEADER lpBIH = static_cast<LPBITMAPINFOHEADER>(lpData);
unsigned nColorSize = lpBIH->biClrUsed * sizeof(RGBQUAD);
if (lpBIH->biBitCount <= 8)
{
    // If lpBIH->bmiHeader.biClrUsed is non zero it specifies the number of colors.
    // If it is zero, all colors are used.
    if (0 == nColorSize)
        nColorSize = (lpBIH->biBitCount << 1) * sizeof(RGBQUAD);
}
// With BI_BITFIELDS compression, use 3 DWORDs.
else if (BI_BITFIELDS == lpBIH->biCompression)
    nColorSize = 3 * sizeof(DWORD);

// Create a BITMAPINFO structure.
LPBYTE lpBiBuf[sizeof(BITMAPINFO) + 255 * sizeof(RGBQUAD)];
LPBITMAPINFO = lpBI reinterpret_cast<LPBITMAPINFO>(lpBiBuf);
// Copy the common content with V4 and V5 headers
::CopyMemory(lpBI, lpBIH, sizeof(BITMAPINFO));
// Adjust the size member
lpBI->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
// Copy the color table
if (nColorSize)
    ::CopyMemory(lpBI->bmiColors, static_cast<LPBYTE>(lpData) + lpBIH->biSize, nColorSize);

HDC hScreen = ::GetDC(NULL);
HBITMAP hBitmap = ::CreateDIBitmap(
    hScreen,              // Create screen compatible bitmap
    lpBIH,                // BITMAPINFOHEADER, BITMAPV4HEADER, or BITMAPV5HEADER
    CBM_INIT,             // Initialize bitmap with data from next parameters
    static_cast<LPBYTE>(lpData) + lpBIH->biSize + nColorSize,
    lpBI,                 // BITMAPINFO (BITMAPINFOHEADER followed by color table)
    DIB_RGB_COLORS);      // Color table uses RGB values
::ReleaseDC(NULL, hScreen);

The created bitmap can be now be selected into a device context and drawn using one of the blitter functions.
 
Share this answer
 
Comments
Rajeshkumar Ramasamy 27-May-13 8:38am    
Jochen,

I am containing the pixel array containing the RGB and Alpha value. I can able to plot the RGB values in display, but Alpha (for transparency) is not working. How can Implement this?

I am using setDIBitsToDevice function to plot the pixel array into display.

Rajesh.
Jochen Arndt 27-May-13 9:22am    
SetDIBits() (which is also called internally by CreateDIBitmap()) will set the high-order byte of 32-bit bitmaps (the alpha value) to 0. GDI originally had no transparency support and it had to be implemented manually (e.g. using masks). Since Windows 2000, the AlphaBlend() function is available. So you should use this function or use GDI+ instead.

You should also edit your question to clarify that you want to draw alpha blended images and remove the misleading BITMAPV4HEADER keyword. Then others may help also because I have not much experience with alpha blending.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900