Groups | Blog | Home
all groups > visual c > november 2005 >

visual c : GDI (-) & Translucent brush


Lloyd Dupont
11/14/2005 12:00:00 AM
In my C# & MC++ app there is some drawing done through GDI (not +, but
win32).
I would like to be able to create a translucent HBRUSH.

the RGB macro create only solid color...
and, anyway, CreateSolidBrush ignore the higher byte.

Any idea?

I though I might do some trick with CreateDIBPatternBrush but I'm not sure
on what to do...

Any tips wellcome!

Michael Phillips, Jr.
11/14/2005 8:54:45 AM
You are on the right track!

1)You could create an alpha channel DIB and select it into
a device context (i.e., use BITMAPV5HEADER).

2)Create your brushes and draw them to the device backed by
the DIB.

3) Using the bits, change the alpha byte to the level of
translucency that you desire.

4) Premultiply the alpha channel against the RGB values.

5) Use the AlphaBlend function to draw your translucent brush.



[quoted text, click to view]

Lloyd Dupont
11/15/2005 12:00:00 AM
hu.....
thanks for your tips!

[quoted text, click to view]

Lloyd Dupont
11/15/2005 12:00:00 AM
mmhh.. well..
There are some problem with this approch.
I created a 1x1 pixel bitmap of the right color, get its HBITMAP< select it
in a memory, screen compatible, DC and AlphaBlend it.

But translucent color get a blue tint.
In fact worst than that, they get a blue tint.. most of the time (but not
always).

Fiu..
I think I will forgot about translucent painting in GDI...

[quoted text, click to view]

Michael Phillips, Jr.
11/15/2005 9:24:11 AM
[quoted text, click to view]

Could you be more specific. Did you follow my steps and
premultiply the alpha channel?

If you don't create and use the bitmap correctly with the
AlphaBlend function, you will get a blue tint.

If you use BitBlt, you will see the blue tinted background.
When you use AlphaBlend correctly with premultiplied
alpha channel, you will not see a blue tint.

There is another approach. You can define a mask to
represent the translucency and use MaskBlt.

You will not be able to define an opacity in the range of 0 -100%
with the MaskBlt approach.

Michael Phillips, Jr.
11/15/2005 6:24:40 PM
You usually premultiply the alpha channel against the RGB colors
when you use AlphaBlend with SourceConstantAlpha = 0xFF.

However the technique that I outlined does not work. I tried creating both
solid and hatch DIB pattern brushes but GDI still ignores the
alpha channel whether I premultiply or set different opacity
levels in the range 0 - 1. I guess you have to roll your own
FillRect or Rectangle function to accept alpha channel brushes.

I believe that GIMP can create translucent brushes.
You may want to download the code to see how they
accomplish the effect.


[quoted text, click to view]

Lloyd Dupont
11/16/2005 12:00:00 AM
Thanks Michael!
Anyway translucent GDI brush is not that essential at this stage. So I just
dropped it.
I'm just a bit frustrated, that's all.

[quoted text, click to view]

Lloyd Dupont
11/16/2005 12:00:00 AM
ok, I will be at work in 2 hours.
could gives me more detail?

what do you mean by:

4) Premultiply the alpha channel against the RGB values.

I created the bitmap in 2 ways.
SD.Bitmap.GetHBitmap()
or as in the code sample in AlphaBlend documentation with CreateDibBitmpa(),
using a BITMAPHEADER (and not a BITMAPV5HEADER)

in the BLENDFUNCTION I used opaque value: 0xff.

could you give me some more tips?

and thanks for your tips so far ;-)


if I set the
[quoted text, click to view]

AddThis Social Bookmark Button