gdi >> Alpha Blending Question

by Gabriele Farina *DarkBard* » Tue, 29 Jun 2004 00:53:55 GMT

Hi,

I'd like to create, using GDI, a Canvas where I can draw any kind of
primitive. I'd like to eneble also alpha blending, so the user can
select a color for the brush but also e percentage of opacity.
I draw on a DC using Draw* primitives, but I saw that brushes do not
support RGBA, so I tried to implement this feature myself, but I got
some problems. When drawing a primitive I have to combine all the pixel
already on the background and with the ones of the new primitive, so I
can retrive the new color to draw. But I can't do that without consuming
a lot of time (drawing all the pixels one by one) ... So I thought to
use Logical functions to combine pixel colors when drawing, but I can't
understand how to implements alpha in this way. Someone can help me??

I don't use GDI directly trought C/C++ code, but I use a Python wrapper
... code is however a good way to make me understand ;)

bye

DarkBard

gdi >> Alpha Blending Question

by Mike D Sutton @ Work » Tue, 29 Jun 2004 01:10:58 GMT


> I'd like to create, using GDI, a Canvas where I can draw any kind of

Have a look at the "GDI blend" demo application on my page which demonstrates a reasonably generic method of drawing
semi-transparently using normal GDI calls.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: XXXX@XXXXX.COM
WWW: Http://www.mvps.org/EDais/

gdi >> Alpha Blending Question

by Tomaz Koritnik » Tue, 29 Jun 2004 02:56:43 GMT

Hi

GDI does not directly support alpha-blending. There is an GDI addon
function called AlphaBlend but this one is limited to drawing bitmaps only.
Something like filling shape with semi-transparent color cannot be done in
GDI. Well, you could create clipping region from shape, create temp-bitmap
painted with one color and then use AlphaBlend to draw it. This will look
just like filling shape with semi-transparent color but will have much
owerhead from bitmap manipulation. The second way is to use DIBs and draw
shaped on them with your own blending routines. But I don't know how and if
it can be done with Python. In case you can directly manipulate memory in
Python, this can be done. The third option is to use GDI+ but this one is
way to slow for some real-time alpha-blending.

regards
Tomaz

gdi >> Alpha Blending Question

by Gabriele Farina *DarkBard* » Tue, 29 Jun 2004 06:11:22 GMT

Tomaz Koritnik ha scritto:


Ok, tnx for your answer.
I understood that if I what to implement a Canvas that supports Alpha
Blending, I have to use Images, drawing primitives on them, setting
alpha and blitting this images on the screen, right?

Now I'd like to understand something:
1) How does Macromedia implements its canvas (the one used in Flash)??
there should be some tricks and tecniquest that I can follow to drawing
my whole area faster ...
2) It's not better to work an and array and then drawing it's content
(converted to and image) on the scrren (or on the DC/backbuffer)???

tnx all

bye

gdi >> Alpha Blending Question

by Tomaz Koritnik » Tue, 29 Jun 2004 21:43:50 GMT

Hi


Almost. For example an ellipse. To fill it with semi-transparent color in
the GDI way, you need to: create elliptical region, create a bitmap with the
size to fit region into, fill bitmap with color, select clip region into
destination DC, draw bitmap using AlphaBlend, draw ellipse with clear brush
for the outline and at last delete the bitmap. O and, two things I have to
mentione: 1) AlphaBlend function is not supported on older windows versions
(NT 4, Win95, maybe Win98) and 2) You need to work with DDB not DIB, because
only with DDBs, AlphaBlend function is hardware acelerated, while on DIBs
not and therefore it's really slow.


Macromedia uses it's own (well actually, they bought it) graphics engine
which uses DIB (which is a memory bitmap and nothing else but a memory
buffer). Graphics engine uses 32bit DIB and it's own routines to draw shapes
and fill them with semi-transparent color. I'm curently developing my own
graphics library which does exactly what you want, fill shapes with
semi-transparent color (beside other things).


This is what you'd do if you'd work with DIBs. You would draw onto DIB
(compose the image) in the background and then, blit it to screen.

regards
Tomaz