Click here to Skip to main content
Click here to Skip to main content

Per Pixel Alpha Blend

By , 20 Apr 2001
 
  • Download source code with sample project - 188 Kb
  • Download ImgDecoder project - 14 Kb
  • Download some test images - 108 Kb
  • Introduction

    In this article I will give you some source code to create per pixel alpha blending windows.

    First, some images:

    Nice, heim?

    For creating a per-pixel window we are going to use:

    • One 24bit Bitmap with alpha channel.
    • The GDI function, UpdateLayeredWindow.

    24bit Bitmap with alpha channel

    The most tricky part of creating the bitmap is finding a simple file format that has an embedded alpha channel.

    The selected file format for the first incarnation of this article was PSP (Paint Shop Pro™ from Jasc®), but now the recommended file format has been changed to PNG (Portable Network Graphics).

    For loading PNG files I use the paintlib library. This library can also load files from PCX, TGA and TIFF formats.

    This demo application does not use paintlib directly; instead it uses a paintlib wrapper dll, ImgDecoder.Dll.

    For loading PSP files I've created a function called LoadPSPFileToDIB(PSPLoader.h/cpp).

    UpdateLayeredWindow

    To use this function we must have a top-level window (in this sample this is implemented by the class CDummyDialog) with the extended style WS_EX_LAYERED set.

    Before we can pass the bitmap to the UpdateLayeredWindow function, we must pre-multiply the rgb channels with the alpha channel. For this you just have to call the PreMultiplyRGBChannels function (take a look at CPerpixelalphaDlg::DoLoadFile).

    From here on read the source code in CPerpixelalphaDlg::DoUpdateDummyDialog.

    Installing & Building

    1. Make sure you have the SDK for Windows® NT 5.0.
    2. Download and extract the source code of this article.
    3. Build and enjoy!

    If you want to build the ImgDecoder.Dll you also have to,

    1. Download paintlib and all the required libraries.
    2. Download the ImgDecoder project.
    3. Extract the ImgDecoder project to the paintlib library.
    4. Build the ImgDecoder project.
    5. Copy the resulting ImgDecoder.dll and ImgDecoder.lib to the bin directory of this article's demo application.

    License

    1. You must mention my name in your application documentation/help box, something like, Portions © 2001 by Rui Godinho Lopes <ruiglopes@yahoo.com>.
    2. I would appreciate it if you send me an email letting me know that you are using this code.
    3. I cannot be held responsible of any kind of data loss caused directly or indirectly by the use of this code - use it at your own risk.
    4. You cannot sell this code - this is free software.

    Don't forget if you have any constructive thoughts just email me!

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Rui Lopes
    Web Developer
    Portugal Portugal
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralMy vote of 5 Pinmemberxueweizhong24-May-13 0:03 
    Generalbeginner code... Pinmemberkilt12-Oct-09 5:39 
    Generalgreat tutorial ! some questions about this still Pinmembernirishere5-Sep-09 0:21 
    QuestionHow about clean WinAPI? Pinmemberflaviusv12-Oct-08 10:16 
    QuestionHow can I draw text on the alpha window? PinmemberGreg Ellis24-Aug-08 10:04 
    QuestionHow to use control in Layered windows Pinmemberpherosiden29-Oct-07 18:29 
    QuestionUrgent (UNICODE) ! PinmemberMutant X1233-Sep-07 20:29 
    GeneralTrouble with headers PinmemberDelfistyaosani28-Jun-07 20:52 
    Generalundeclared identifiers PinmemberDV6413-Apr-07 8:09 
    GeneralMore Test Images Pinmemberninadpachpute12-Feb-07 22:05 
    JokeGR8 STUFF!!! PinmemberDieZeL25-Jul-06 23:09 
    QuestionHow to add control to the dialog? Pinmemberblackgole20-Jul-06 15:47 
    GeneralEfficency of layered windows Pinmemberjens pedersen9-Jun-06 3:40 
    QuestionWriting Text PinmemberMrBlob31-May-06 14:29 
    QuestionUpdateLayeredWindow Pinmembertoto777toto9-Apr-06 16:14 
    GeneralDon't use this! PinmemberGeorge9876543213-Mar-06 5:23 
    GeneralUpdateLayerdWindow not recognised Pinmemberananasgroup24-Mar-05 12:37 
    Generalplease help Pinmemberv_ivan12-Dec-04 23:52 
    GeneralUpdateLayeredWindow Pinmemberv_ivan27-Aug-04 0:31 
    GeneralLots of images Pinmemberartombo14-Jul-04 15:43 
    GeneralDo not see any Alpha Blending. PinmemberWREY7-Oct-03 10:23 
    GeneralPer-Pixel Alphablended ActiveX Controls PinmemberEric Hansen2-Oct-03 14:01 
    GeneralAlpha Blended ActiveX Controls PinmemberEric Hansen2-Oct-03 14:00 
    GeneralHelp - 'AC_SRC_ALPHA' : undeclared identifier PinmemberDataDuck18-Jun-03 6:00 
    GeneralUnicode compilation PinsussAmit Sindhwani31-Oct-02 3:46 
    GeneralControls on the Dialog Pinmemberazmodius30-Aug-02 20:14 
    GeneralPCX in 2 bit Pinmembernileshwk8-Aug-02 5:11 
    GeneralPremultiply PinsussSibilant22-Jul-02 20:21 
    QuestionWin98/ME..? PinmemberMario M.15-Feb-02 6:28 
    GeneralPlatfrom SDK problem PinmemberJanssens Johan10-Oct-01 13:47 
    GeneralPer Pixel Alpha w/ DirectDraw8 PinmemberNick Alexeev7-Jun-01 12:51 
    QuestionUsing controls on the dialog? PinmemberAnonymous24-May-01 10:55 
    GeneralDoes work in 16bpp but not in 32bpp PinmemberRamon Smits22-Apr-01 9:33 
    GeneralWS_EX_LAYERED & SDK PinmemberJean-Marc Molina21-Apr-01 10:36 
    GeneralGreat ! I love it PinmemberAnonymous17-Apr-01 7:16 
    QuestionWhy not add PNG support? PinmemberRamon Smits3-Apr-01 22:45 
    GeneralDowngraded the PSP sample image PinmemberRui Lopes3-Apr-01 1:00 
    Generalcan't open psp files PinmemberMaximilian Hänel1-Apr-01 9:08 
    QuestionVery cool, but...PSP? PinmemberAnonymous1-Apr-01 6:28 
    GeneralA couple of questions.... PinmemberChristian Graus31-Mar-01 12:45 
    GeneralPSP reading PinmemberChris Losinger31-Mar-01 9:17 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.6.130619.1 | Last Updated 21 Apr 2001
    Article Copyright 2001 by Rui Lopes
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid