none
AlphaBlend on 64bit Windows 7 Not Working

    Question

  • I'm running Microsoft Visual Studio 2008 on a Windows 7 32bit computer and building my project for x86 machines.  So, when my program is installed on a Windows 7 64bit computer, its installed into the Program Files (x86) folder.  The problem is I'm using the Windows GDI AlphaBlend function to draw semi-transparent rectangles over a DC object.  It looks great on Windows XP and Windows 7 32 bit but when installed on a 64bit machine running Windows 7, the rectangles are completely transparent.  Does anyone have any suggestions on how I fix this problem?  I appreciate the advice!
    Friday, December 03, 2010 7:56 PM

Answers

  • Hi,

    Welcome to App Compat for Windows Development forum!

    As far as I know, Windows GDI AlphaBlend can behave differently 32bit and 64bit Windows.   It's a known issue which may be fixed in the next version of Windows, Windows 8.   Currently, you can try the following workaround to make it work in 64bit Windows:

    We can set AlphaFormat to 0 if your bitmap does not have an alpha channel, if you are using a DDB (no alpha channel).

    Besides, if it does not fix the issue, you can provide us with more detailed information like some sample codes, for further investigation.

    Good day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 06, 2010 2:55 AM
    Moderator

All replies

  • Hi,

    Welcome to App Compat for Windows Development forum!

    As far as I know, Windows GDI AlphaBlend can behave differently 32bit and 64bit Windows.   It's a known issue which may be fixed in the next version of Windows, Windows 8.   Currently, you can try the following workaround to make it work in 64bit Windows:

    We can set AlphaFormat to 0 if your bitmap does not have an alpha channel, if you are using a DDB (no alpha channel).

    Besides, if it does not fix the issue, you can provide us with more detailed information like some sample codes, for further investigation.

    Good day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 06, 2010 2:55 AM
    Moderator
  • Thanks for the suggestion.  Unfortunately, changing the AlphaFormat to 0 did not fix my problem, everything appeared the same on the 64bit computer and the "background" image appeared much darker on 32bit computers.

     

    Is there a way I can just check if the computer is 32bit, do this, else do something else?  This way I'd use the AlphaBlend function on 32bit computers but use a simpler, not-as-fancy function on 64bit computers.

     

    Thanks again,

    Susan

    Monday, December 06, 2010 5:00 PM
  • Looks like I spoke too soon, on a second 64bit Windows 7 PC (which was exhibiting the same problems with the AlphaBlend function before), the work-around did work (background is dark but I could look into that, its the same on 32bit and 64bit).

    No idea why its not working on the other 64bit computer though, both computers have Visual Studio on them and can compile and build the project so I can't imagine its a missing dll.  Anyways, I'm just glad its fixed on one of the computers, for now...

    Thanks for your help!

    Monday, December 06, 2010 7:22 PM
  • You're welcome, Susan!

    For how to check whether it's a 32bit or 64bit Windows system, you can refer to the sample code CppCheckOSBitness in All-In-One Code Framework.   We have detailed ReadMe file to introduce this sample. 

    If you have questions, feel free to let me know.

    Good day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 07, 2010 2:06 AM
    Moderator
  • Hi Susan,

    How is the problem now?  If you need any further assistance, please feel free to let me know.

    Good day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 09, 2010 1:22 AM
    Moderator
  • Its looking pretty good now, still looks a little better on 32bit machines, with the background getting darkened a little on 64bit machines and the transparency needing to be set a little higher on the "top" image (so that the background doesn't get too dark), but all in all in much better.  I just check to see if the directory "Program Files (x86)" exists, if it does, I set the AlphaFormat to 0 and the SourceConstantAlpha to 75, otherwise I set the AlphaFormat to AC_SRC_ALPHA and the SourceConstantAlpha to 200.  I know checking for that directory isn't the safest thing, but its working so far...  As for that second 64bit PC that was behaving weird, after a couple restarts and reinstallations, it finally started behaving properly, so I don't know what was going on there, but maybe it was just something wrong with the installer.

     

    Anyways, thanks a lot for your help.  I'm glad I posted my question here!

    Thursday, December 09, 2010 1:59 PM
  • Sure, I'm glad to hear the problem is solved.  MSDN forum is amazing!  We have learn a lot here everyday.  :)  

    Have a nice weekend!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 10, 2010 2:14 AM
    Moderator
  • I'm having a similar problem with AlphaBlend on a 64-bit Windows XP (SP3) system (running Compaq Fortran).

     

    The bottom line here is that you can't use per-pixel alpha (with AlphaBlend) on 64-bit systems, right ?

     

    Does anyone know how to do per-pixel alpha on 64-bit systems (using standard WinAPI routines) ?

    Friday, March 25, 2011 9:20 PM
  • This is the code I'm currently using, based on Lingzhi Sun's suggestions.  It doesn't look as good on 64-bit PCs as on 32-bit PCs, but its not too far off:

     

        // Now alpha blend objects back to main DC
        BLENDFUNCTION bfn = {0}; // Struct with info for AlphaBlend
        bfn.BlendOp = AC_SRC_OVER;
        bfn.BlendFlags = 0;
        if(fIsX64)
        {
            // this isn't great, and causes problems on 32bit PCs (if you use it on them also),
            // but it's better than current alternatives on 64bit machines
            bfn.SourceConstantAlpha = 75;
            bfn.AlphaFormat = 0;
        }
        else
        {
            // this draws empty overlaid regions on 64bit machines but looks great on 32bit PCs
            bfn.SourceConstantAlpha = 200;
            bfn.AlphaFormat = AC_SRC_ALPHA;
        }

     

    On a side note, for a separate project that I am working on, I am using QT with QPixmap (for background image), QPainter (to draw overlays on QPixmap) and QLabel (to display QPixmap) classes to draw semi-transparent regions on top of a background image and it looks great.  You just set the colors of the regions with alpha values and they are drawn over the background perfectly.

    Friday, March 25, 2011 9:48 PM
  • Your "if(fIsX64)" case uses a single alpha value (75) for the whole bitmap, and that does seem to work on 64 bit systems.

    The "else" case uses a global value (200) combined with individual alpha values for each pixel (in the 4th byte of the pixel dw), and this is handy for a lot of things.

    For example, if you want to place a circular image over an existing "background" bitmap, you can draw the circle on a new square bitmap and assign alpha = 0 for all the pixels outside the circle. Then call AlphaBlend with bfn.SourceConstantAlpha = 255; and bfn.AlphaFormat = AC_SRC_ALPHA;

    As you say, the problem is this doesn't work on 64 bit systems. What I have done to fix this is to use GetDIBits to load the bitmaps into RAM arrays and then I can do the alpha math myself:

    red_bkg    = A * red_circle    + B * red_bkg

    green_bkg = A * green_circle + B * green_bkg

    blue_bkg   = A * blue_circle   + B * blue_bkg

    where A = alpha/255 and B = 1 - A.

    Then call SetDIBits to write the modified bits back to the background bitmap. This bypasses the AlphaBlend function, and so works fine on 64 bit systems.

     

    Monday, March 28, 2011 4:23 PM