|
|
Alpha PlayI rarely use Windows 2000 or Windows XP-only API functions since my apps usually have to run on ancient (?) systems such as Windows 98. Hopefully this will change soon enough when every home-user decides to retire their Win98 box (yearh right; as of 2005 this still seems a long way into the future), but it's not like the latest versions of Windows introduced a lot of GUI stuff to play with anyway.Since I don't use these functions much, I thought I'd write down some notes on their use. TransparentBltSome of the few new APIs released were bundled in themsimg32 dll and
were probably part of Internet Explorer's effort to paint pretty web pages.
One of them is TransparentBlt which should have been added to Windows a long time
ago. It simply does a standard blit but uses a colour-key to mask out all colour-key
coloured pixels.
The function operates just like the regular BitBlt except that it takes a color-key
so I'm not going into details with it.
GradientFill
Somewhat more interesting is GradientFill that can be instructed to paint a smoothly interpolated
colour-band. I did that by hand in the old days, but this is much better as it
even looks good on low-end displays (64K colours and below).
GradientFill takes some arcane arguments and shades both triangles and rectangles.
With this little setup we can draw a neat gradient band:
AlphaBlendAlphaBlend is where the fun starts. This Win2000 API allows you to blit your image with
dynamic transparency. You can blit an image with 30% transparency to an existing device context and blend
the images.
If you just alpha-blit an image to your device context, you will probably be disappointed.
...which produces this result with 30% transparency:
The function actually behaves correctly because what you really want in this case is to specify a colour-key (masked pixels). Unfortunately the function doesn't take one as argument - but instead you can use the function with pr-pixel alpha. In this scenario the source bitmap itself contains a transparency value for each pixel (alpha-channel) where fully transparent pixels have 0% alpha and the visible parts could be 100% for fully opaque or 30% to blend it with the background.
This is really cool but also where the problems begin. First of all, you will either need to predefine the
alpha values in your image or generate the alpha values on the fly.
To load the bitmap resource always remember:
...because LoadBitmap is stuck in Windows 3.1 mode and is now defunct.
Now, all this assumes that you are able to create the alpha layer and save a 32bpp BMP file in your
favourite bitmap editor. Unfortunately not even my trusted Paint Shop Pro was able to do that.
The suggestion is then to use another file format such as PNG, but that would require inclusion
of yucky image libraries.
Anyway, creating a bitmap with alpha-values by hand does require a bit of typing, so here are the steps to follow: First we need to know the dimensions of our bitmap.
The NULL argument value in there forces Windows to just return the bitmap
size information.
We now have the width and height, so let's ask Windows to give us access to the pixel data, this time in a format we can work with:
It is important to specify which colour codec and depth you want the pixels in otherwise
Windows just returns the format as it was read from disk.
We then ask Windows to generate a new bitmap with the same dimensions and 32bpp codec which we will populate with the same pixel data and the new alpha values:
The DIB Section gives us access to each individual pixel in a memory buffer.
We even asked for 32bpp so Windows kindly made room for the extra Alpha byte in the pixel data.
Now we can generate the alpha values. In this sample we just scan all the pixels and manipulate a colour-key. Since we just created a new empty bitmap we'll have to copy the source image pixels too as we go.
Here we just set the alpha value to 255 (0xFF000000 in ARGB pixel-format) to allow pixels
to become opaque.
Finally we can blit the new image to the device context. We'll need to tweak the arguments for AlphaBlend slightly because we are now using pr-pixel alpha.
This does a pr-pixel alpha blit. The argument iAlpha defines how semitransparent
the blit should be. 255 is opaque; at 30 the image is blended with the background and barely visible.
Working with alpha may not always be as fast as you could hope.
You can detect if the system has acceleration on the graphics card by
using
I bundled the code and polished it a bit in the download available below.
Alternatively, you may consider using the GDI+ library. After all it does support many of these operations with trivial implementation. But since Microsoft doesn't seem to want to push for graphics card acceleration for the entire library, you will probably have to live with its sluggish and slow performance for some time to come. Premultiply your bitmapSo theAlphaBlend API is cool, and I did mention that if you already had
a bitmap with the proper alpha-channel you were good to go. Well, it's not always
that simple. The AlphaBlend expects your bitmap data to have its
RGB data premultiplied with the alpha channel. That basically means that the bitmap
must be prepared for quick blitting to the screen and the process of doing so
is very similar to the sample code I just showed above.
There is source code in the sample download.
Just remember that if your alpha bitmap doesn't look as you expected it, you may need
to preprocess the data before you use the bitmap handle.
Source Code DependenciesMicrosoft Visual C++ 6.0Microsoft WTL 7.5 Library Download Files
Written by Bjarke Viksoe. Article submitted 11/13/2005. To the top
|
|||