1

I have a bitmap which background needs to be replaced with part of another bitmap. Everything works fine until I enable ClearFont on my WindowsXP.

In order to explain my problem better, let us label first bitmap as bmpDestination and second as bmpSource.

Here is how the bmpSource looks like :

enter image description here

Here is how bmpDestination looks like :

enter image description here

When ClearType is off, here is how the correct result looks like :

enter image description here

And here is the incorrect result of their combining when ClearType is on:

enter image description here

ClearType alters some parts of the text background color, so they aren't white anymore ( RGB( 255, 255, 255 ) ) but a combination of white and text color.

I am using BitBlt() and monochrome bitmap to create a mask, and to simulate transparency. I have tried using TransparentBlt() too, but got the same result.

How can I combine bmpSource and bmpDestination, when ClearType is enabled, so I can create correct result like above ?

Thank you for your help.

Best regards.

AlwaysLearningNewStuff
  • 2,939
  • 3
  • 31
  • 84
  • 6
    ClearType blends the foreground and background pixels. In your case, the backgrounds is white. You then try to transfer those pixels to a colored background, so the blending is now all wrong. You need to draw the text directly on the final background. – Raymond Chen May 11 '14 at 00:52
  • @RaymondChen: *You need to draw the text directly on the final background*-unfortunately that is not possible in my case, hence the reason for posting this question. – AlwaysLearningNewStuff May 11 '14 at 00:55
  • 1
    (Damn, that tree view custom draw problem again!) Well I'm afraid you can never make a ClearType rendering usable for this kind of composition again -- it just adds so much more than just shades between text color and background color, you can't filter that out automatically even in a full-fledged image manipulation program. – Yirkha May 11 '14 at 01:12
  • It just isn't going to work. You have to either turn off ClearType or draw the text directly onto the background. I don't know why you say that isn't possible, but if it's not possible, then it's not possible. – Cody Gray - on strike May 11 '14 at 01:14
  • @CodyGray: [This question](http://stackoverflow.com/questions/23432110/incorrect-result-with-transparentblt?rq=1) explains where I get the `bmpDestination`. [*Custom draw*](http://stackoverflow.com/questions/22879703/treeview-node-with-transparent-text-background-instead-of-default-white) doesn't help, so the method from the previous link was my only chance. Everything works fine when I use standard anti-aliasing, so I guess I will have to create and select my own font which has *ClearType* disabled. I just hoped someone could come up with a solution to this last piece of the puzzle... – AlwaysLearningNewStuff May 11 '14 at 01:20
  • 1
    Instead of doing your masked BitBlt, use a memory DC and render the same gradient background into it as you use in your treeview. Then draw the text transparently over the top of the memory DC, and finally do a normal (not masked) blit from the memory DC to the tree. – Jonathan Potter May 11 '14 at 07:49
  • @JonathanPotter: But I do not know how to draw treeview item's text at the proper coordinates in memory DC, that is the point. Furthermore, I would need to manually draw state images as well, wouldn't I ? – AlwaysLearningNewStuff May 11 '14 at 08:48

1 Answers1

2

Render the treeview with black text on a white background. Use a font with grey-scale anti-aliasing. Don't use ClearType anti-aliasing. I'm moderately sure you can achieve this with one of the fdwQuality parameters to CreateFont, but I wouldn't swear to it.

Each pixel will have a shade of grey between white and black. You can interpret this as transparency. White is fully transparent; black is fully opaque. Use this information to create a bitmap with transparency. Render this transparent bitmap over your multi-coloured background.

arx
  • 16,686
  • 2
  • 44
  • 61
  • I have checked documentation for `CreateFont` and found nothing for grayscale font. However, I did find a flag `ANTIALIASED_QUALITY` and tried to create font with it. After setting it as a treeview font everything rendered fine. Still, these are all workarounds for the problem with *ClearType*... I guess there is no solution for this, only a workaround... Upvoted. Thank you. Best regards. – AlwaysLearningNewStuff May 13 '14 at 11:40