how can I display a bitmap with an alpha chanel?
-
I want to display a bitmap that has an alpha chanel for the background. How can I make the alpha chanel transparent? I tried using AlphaBlend() but it doesn't work. Any suggestions?
danginkgo
-
Hi See if this helps you: bitmap - alpha channel[^]
-- "Programming is an art that fights back!"
-
I want to display a bitmap that has an alpha chanel for the background. How can I make the alpha chanel transparent? I tried using AlphaBlend() but it doesn't work. Any suggestions?
danginkgo
danginkgo wrote:
I tried using AlphaBlend() but it doesn't work.
How did you do this? What values did you use in the BLENDFUNCTION struct? Is the bitmap 32bpp? What does "doesn't work" mean.
-
danginkgo wrote:
I tried using AlphaBlend() but it doesn't work.
How did you do this? What values did you use in the BLENDFUNCTION struct? Is the bitmap 32bpp? What does "doesn't work" mean.
This is my BLENDFUNCTION struct:
BLENDFUNCTION blendF; blendF.BlendOp = AC_SRC_OVER; blendF.BlendFlags = 0; blendF.SourceConstantAlpha = 255; blendF.AlphaFormat = AC_SRC_ALPHA;
The bitmap is 32 bpp with an alpha chanel. When I display the image using AlphaBlend() function the image looks the same as when I displayed it using BitBlt(). I want the background (wich has an alpha chanel) to be transparent. Is it posible using this function?danginkgo
-
Hi, Here is an article explaining alpha blending: Alpha Blending using GDI+[^] Regards, Suman
-- "Programming is an art that fights back!"
-
I am using GDI. Is alpha blending posible only with GDI+ ? I want to blend only the background. Thank You for the answers.
danginkgo
HI I dont know the differences between GDI & GDI+, but you can do it using GDI. Refer here: Windows GDI - Alpha Blending[^] A sample here: Alpha Blending a Bitmap[^]
-- "Programming is an art that fights back!"
-
This is my BLENDFUNCTION struct:
BLENDFUNCTION blendF; blendF.BlendOp = AC_SRC_OVER; blendF.BlendFlags = 0; blendF.SourceConstantAlpha = 255; blendF.AlphaFormat = AC_SRC_ALPHA;
The bitmap is 32 bpp with an alpha chanel. When I display the image using AlphaBlend() function the image looks the same as when I displayed it using BitBlt(). I want the background (wich has an alpha chanel) to be transparent. Is it posible using this function?danginkgo
danginkgo wrote:
When I display the image using AlphaBlend() function the image looks the same as when I displayed it using BitBlt().
Then all the alpha channel values must be 255.
danginkgo wrote:
Is it posible using this function?
Yes. Try something like this, creating and rendering your own 32bpp ARGB bitmap:
LONG lImageWidth = 640;
LONG lImageHeight = 480;
WORD wBitsPerPixel = 32;LONG lStride = lImageWidth * 4;
BYTE* pBitmapBits;
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = lImageWidth;
bmi.bmiHeader.biHeight = lImageHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = wBitsPerPixel;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = lStride * lImageHeight;
//bmi.bmiHeader.biXPelsPerMeter = 0;
//bmi.bmiHeader.biYPelsPerMeter = 0;
//bmi.bmiHeader.biClrUsed = 0;
//bmi.bmiHeader.biClrImportant = 0;HDC hdcMem = ::CreateCompatibleDC(0);
HBITMAP hBitmap = ::CreateDIBSection(hdcMem, &bmi, DIB_RGB_COLORS, (void**)&pBitmapBits, NULL, 0);
if (hBitmap)
{
memset(pBitmapBits, 0, bmi.bmiHeader.biSizeImage);
RGBQUAD *pCurPixel = (RGBQUAD *)pBitmapBits;
int PixelCount = lImageWidth * lImageHeight;
while (PixelCount > 0)
{
(*pCurPixel).rgbRed = 0x00;
(*pCurPixel).rgbGreen = 0x00;
(*pCurPixel).rgbBlue = 0xA0;
(*pCurPixel).rgbReserved = 0x80;
pCurPixel++;
PixelCount--;
}HGDIOBJ hOldBitmap = ::SelectObject(hdcMem, hBitmap); HDC hClientDC = ::GetDC(\*this); HPEN hPen = ::CreatePen(PS\_SOLID, 10, RGB(255,0,128)); HGDIOBJ hOldPen = ::SelectObject(hClientDC, hPen); ::MoveToEx(hClientDC, 0, 0, NULL); ::LineTo(hClientDC, 650, 490); ::SelectObject(hClientDC, hOldPen); ::DeleteObject(hPen); BLENDFUNCTION bf; bf.BlendOp = AC\_SRC\_OVER; bf.BlendFlags = 0; bf.SourceConstantAlpha = 0xFF; bf.AlphaFormat = AC\_SRC\_ALPHA; ::AlphaBlend(hClientDC, 0, 0, lImageWidth, lImageHeight, hdcMem, 0, 0, lImageWidth, lImageHeight, bf); ::ReleaseDC(\*this, hClientDC); ::SelectObject(hdcMem, hOldBitmap); ::DeleteObject(hBitmap);
}
::DeleteDC(hdcMem);