CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2002
    Location
    Colorado, U.S.A.
    Posts
    980

    32bit Bitmap and AlphaBlend

    I have the raw data for several 32bit images (RGB + alpha). How do I create a HBITMAP so that I can blend these images and then display them to the screen? I've been reading through all the bitmap functions on the MSDN, but I can't see how to create a bitmap with a different color depth than the screen. Can anyone point me to the correct functions? Thanks a lot!

    Kelly

  2. #2
    Join Date
    Nov 2004
    Location
    Virginia, The lovers' state
    Posts
    64

    Re: 32bit Bitmap and AlphaBlend

    There are ways to do this. But I think GDI is not the best service to use for alpha blending.

    I do alpha blending using directX (Direct3D) - it is faster and more feature rich.

    I could give links to alpha blending using Direct3D but I am guessing you need to begin by looking up Direct3D on MSDN..

    hth

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: 32bit Bitmap and AlphaBlend

    I do alpha blending using directX (Direct3D) - it is faster and more feature rich.

    I could give links to alpha blending using Direct3D but I am guessing you need to begin by looking up Direct3D on MSDN..
    Actually, With Win2000 and WinXP Support for desktopmode alphablending has been introduced. Alphablending is no longer available only in the realm of Direct3D (DirectDraw does not have native alphablending, only colorkeying).

    Desktop alphablending is not so much about doing fast animation with alphablending, of flashy 3D transparencies, but rather about doing visual effects on screen (on desktop).

    Nonrectangular windows used to require regions (slow) and are now MUCH MUCH faster using alphablending.

    The rounded 'titlebar' in WinXP is achieved through alphablending.
    The infoballoons also use alphablending.
    The fadein/fadeout menu's, toolbars...
    The shadow effect around the menu's, toolbars, balloons... are also alphablending.

    When possible, videohardware is used to achieve the alphablending, if not, it goes through (slow) software processes.

    However, much of GDI still does not have alphablending support. Example: If you draw a line, to the best of my knowledge, it is always 100% opaque, you can't create a pen with a alphachannel. I really wish they would have supported RGBA() type colors (instead of just RGB()). sigh...


    Anyhoo...
    To create a HBITMAP with a colordepth different from the screen, use CreateDIBSection().

  4. #4
    Join Date
    Jun 2002
    Location
    Colorado, U.S.A.
    Posts
    980

    Re: 32bit Bitmap and AlphaBlend

    Thanks for the replies. Whenever possible, our application uses OpenGL to do all of our drawing. However, there is a class of computers that either doesn't support GL, or doesn't support GL with texture blending and enough texture units. We are trying to be able to fall back to GDI to do a subset of our current drawing. We basically need to do this:

    1. Create 3 32bpp bitmaps (regardless of the users currently set color depth).
    2. Blend the 3 images.
    3. Draw them to the screen.

    Earlier we did some drawing with GDI, blending two images together based on a constant alpha value. Now we need to be able to blend based on a constant value and a per pixel value. I would love to be able to use GDI+ for this (from the few things I've done with it, it seems much easier to use, especially for doing alpha blending) but I haven't been able to figure out if there's a way to do alpha blending in GDI+ that supports a constant alpha value along with a per pixel alpha.

    OReubens:
    With CreateDIBSection, it looks like we have to use the array that is returned for our data. Is that correct? Is there a way to create a 32bpp bitmap using data that we already have in our own array?

    Kelly

  5. #5
    Join Date
    Jun 2002
    Location
    Colorado, U.S.A.
    Posts
    980

    Re: 32bit Bitmap and AlphaBlend

    Another question:

    Assuming I have a 32bpp DIB, can I just do a BitBlt to a screen dc, or do I need to convert it to a DDB first (using CreateDIBitmap)?

    Thanks again!

    Kelly

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: 32bit Bitmap and AlphaBlend

    [ Moved thread ]

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: 32bit Bitmap and AlphaBlend

    Quote Originally Posted by Runt888
    Assuming I have a 32bpp DIB, can I just do a BitBlt to a screen dc, or do I need to convert it to a DDB first (using CreateDIBitmap)?
    I'm not sure I understand your question...
    Are you talking about a 32bit DIB imagefile you wish to load into a memoryDC ? or ?

    Typically, you would have to get the Dibsection (correctly) into a memoryDC (with a possible conversion from whatever format into 32Bit RGBA DDB). Then you would use the AlphaBlend() GDI function to do the actual blitting to screen.

    BitBlt() does not do alphablending even if the source image contains an alphachannel.

    So typically what you would need to do is (replace blue stuff with your stuff ):
    Code:
    HDC hMemDC = CreateCompatibleDC(<WindowDC>);
    
    BITMAPINFO bmi;
    ZeroMemory(&bmi, sizeof(BITMAPINFO));
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = <BitmapWidth>;
    bmi.bmiHeader.biHeight = <BitmapHeight>;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;
    bmi.bmiHeader.biSizeImage = bmi.bmiHeader.biWidth * bmi.bmiHeader.biHeight * 4;
    
    VOID *pvBits;
    HBITMAP hBmp = CreateDIBSection(hMemDC, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0);
    SelectObject(hMemDC, hBmp);
    
    // Load/blit/create bitmap into 'pvBits'
    
    BLENDFUNCTION bf;
    bf.BlendOp = AC_SRC_OVER;
    bf.BlendFlags = 0;
    bf.SourceConstantAlpha = 0xFF;  // 0x00 (transparent) through 0xFF (opaque)
                                    // want bitmap alpha, so no constant.
    bf.AlphaFormat = AC_SRC_ALPHA;  // Use bitmap alpha
    
    AlphaBlend(<WindowDC>, WindowWidth, WindowHeight, bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight, hMemDC, 0, 0, bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight, bf));

  8. #8
    Join Date
    Jun 2002
    Location
    Colorado, U.S.A.
    Posts
    980

    Re: 32bit Bitmap and AlphaBlend

    Thanks for the quick reply! That answers my questions.

    Kelly

  9. #9
    Join Date
    Feb 2009
    Posts
    1

    Re: 32bit Bitmap and AlphaBlend

    Hi,
    I have the similar problem.
    I m developing a multiwindow appln in Win32.
    When a particular window comes in front i want tht the window shud look like a transparent window.
    I tried ur code but it didn't work.
    Plz Help me.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured