Windows XP
(1)
Windows Vista
(1)
Vista
(1)
XP
(1)
CreateCompatibleDC
(1)
GetStockObject
(1)
SetTextColor
(1)
SetBkColor
(1)

TextOut to a 32-bit bitmap for AlphaBlend on Vista x64

Asked By akiril
27-May-08 12:04 AM
Hello,

Problem: can't output text by TextOut (or ExtTextOut or DrawText) to a
bitmap, created with CreateDIBSection, so that bitmap can later be AlphaBlend
to a paint DC.
In pseudo code:
1. paintDC = BeginPaint()
2. compatibleDC = CreateCompatibleDC (paintDC)
3. bitmap = CreateDIBSection (32 bit, 1 plane, BI_RGB compression,
biSizeImage=0)
4. Select bitmap into the compatibleDC
5. fill the bitmap with some ARGB color
6. TextOut in any mode (SetBkMode) with any back color (SetBkColor) and any
text color (SetTextColor).
7. AlphaBlend compatibleDC into paintDC with AlphaFormat=AC_SRC_ALPHA

A string, used on step 6, is not visible when executed on Vista x64 (no
composition).

Note: steps 1-5, 7 are taken from Alpha Blending a Bitmap
(http://msdn.microsoft.com/en-us/library/ms532295(VS.85).aspx)

The real application uses a compatibleDC as an offscreen and drawing
directly to the paintDC is almost not an option.

Test systems: 32-bit XP SP2 and 64-bit Vista Business. Several other 64-bit
systems (some Intel-, some AMD-based) has the same problem, hence video
driver does not seem to contribute to a problem.

So, is it a known "limitation"? Or am I missing something obvious?

Side question: on 32-bit XP SP2, the behaviour of TextOut (step 6) is
different, depending on which brush is used during class registration. For
example, wcex.hbrBackground= (HBRUSH)GetStockObject(BLACK_BRUSH); works, but
(HBRUSH)(COLOR_WINDOW+1) causes the string to appear in a wrong color on a
wrong background.

Thank you for your time!

--
Kirill.

Thats because GDI functions mostly only know about RGB, not ARGB.

Asked By Somebody
27-May-08 02:08 AM
Thats because GDI functions mostly only know about RGB, not ARGB. *MOST* GDI
functions will set the A channel to 0 and thus make your text transparent.
You'll find even basic functions like FillRect() and SetPixel() don't work
correctly when writing to ARGB surfaces.

So the way you need to do it is to build your bitmap on a 24bit RGB surface,
then after you are done, loop through and set the A channel to 0xff and then
AlphaBlend() and WS_EX_LAYERED will work as expected.

Is there any reason why you cannot switch steps 6 and 7?

Asked By Michael Phillips, Jr.
27-May-08 09:21 AM
Is there any reason why you cannot switch steps 6 and 7?

The AlphaBlend function requires that the pixels in the bitmap be
premultiplied by the alpha channel.

If the alpha channel contains a pixel color greater than zero and not 255,
then the rgb channels will have their color intensity changed.

Your text will have its rgb color intensity altered in those areas of the
bitmap.

If the alpha channel is 0, then those rgb channels in the bitmap will be
completely transparent.  Your text will disappear, if it falls within those
areas.

Hello,"Somebody" wrote...

Asked By Kirill
27-May-08 10:13 AM
Hello,

It's true and I'm aware of that, but unfortunately it does not explain why
the code works correctly under 32-bit Windows XP.

I thought about this as well and it indeed fixes the problem. Unfortunately,
I'm not sure how well it will work with TextOut and ClearType (seemingly,
the latter may use alpha channel).

--
Kirill.
Michael,"Michael Phillips, Jr." wrote in message...Well...
Asked By Kirill
27-May-08 10:21 AM
Michael,

Well... in a sample application it does fix the problem. But in the real
application steps 2 (using window's DC) through 6 are not a part of WM_PAINT
event handler. And it would be difficult to bring step 6 into WM_PAINT.

I've read about that, and even though it does not explain why it works under
32-bit Windows XP, your comment makes me wonder who's responsible for
pre-multiplying? If a bitmap is supposed to be pre-multiplied by the caller,
then 64-bit Windows Vista is very likely to be broken, because I verified
the pixels' values and in a simple test they're all relatively good (e.g.
0x00FFFFFF when drawing with white). If the pre-multiplication is done
inside of AlphaBlend, then looks like TextOut is broken on 64-bit version,
because it seemingly fills alpha with 0.

--
Kirill.
TextOut to a 32-bit bitmap for AlphaBlend on Vista x64
Asked By jeta
27-May-08 10:22 PM
Hi Kirill,

Can you send an email to me at: jetan@online.microsoft.com(remove

Best regards,
Jeffrey Tan
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Post Question To EggHeadCafe