tags:

views:

482

answers:

1

Hi , i have created a Bitmap using GDI+.I am drawing text on to that bitmap using GDI Drawtext.Using Drawtext i am unable to apply tranparency. Any help or code will be appreciated.

+3  A: 

If you want to draw text without a background fill, SetBkMode(hdc,TRANSPARENT) will tell GDI to leave the background when drawing text.


To actually render the foreground color of the text with alpha... is going to be more complicated. GDI does not actually support alpha channels all that widely in its APIs. Outside of AlphaBlend actually all it does is preserve the channel. Its actually not valid to set the upper bits of a COLOREF to alpha values as the high byte is actually used for flags to indicate whether the COLOREF is (rather than an RGB value) a palette entry.

So, unfortunately, your only real way forward is to:

  1. Create a 32bit DIBSection. (CreateDIBSection). This gives you an HBITMAP that is guaranteed to be able to hold alpha information. If you create a bitmap via one of the other bitmap creation functions its going to be at the device colordepth that might not be 32bpp.
  2. DrawText onto the DIBSection.
  3. When you created the DIBSection you got a pointer to the actual memory. At this point you need to go through the memory and set the alpha values. I don't think that DrawText is going to do anything to the alpha channel by itself at all. Im thinking a simple check of the RGB components of each DWORD of the bitmap - if theyre the forground color, rewrite the DWORD with a 50% (or whatever) alpha in the alpha byte, if theyre the background color, rewrite with a 100% alpha in the alpha byte. *
  4. AlphaBlend the bitmap onto the final destination. AlphaBlend requires the alpha channel in the source to be pre-multiplied.

* It might be sufficient to simply memset the DIBSection with a 50% alpha before doing the DrawText, and ensure that the BKColor is black. I don't know what DrawText might do to the alpha channel though. Some experimentation is called for.

Chris Becke
Thanks for your reply.I have used this method.Using this the text is drawn on to rectangle tranparently.But i want to apply transparency(alpha) to the text drawn which is not possible using SetTextColor method. As SetTextColor ignores the alpha value of a Color.I have gone through some application which uses mask bitmap for applying tranparency , But i haven't get desired results. and i don't want to apply tranparency to the bitmap , I want to apply transparency to the Text drawn on to the Bitmap. Any help is appreciated
VideoDev
Th idea is, draw onto a black bitmap, not a bitmap with information aleady on it. After blitting the text you can then scan the bitmap for non black pixels and adjust the alpha channel for each pixel based on whether or not its part of the text.
Chris Becke