Link to home
Start Free TrialLog in
Avatar of eugenem
eugenem

asked on

TransparentBlt for DIBs (256 color)

I need to paint DIBs transparently (by transparent color).
There is TransparentBlt for DDBs on www.codeguru.com but not for DIB. Can I convert DIB to DDB and then perform TransparentBlt? Will I lose right colors?
Avatar of chensu
chensu
Flag of Canada image

What you should do is to convert a DIB to a DDB so that you get the HBITMAP and HPALETTE and pass these two handles to the TransparentBlt. And process the WM_QUERYNEWPALETTE and WM_PALETTECHANGED message sent to the top-level window. Thus you won't lose right colors.
Avatar of eugenem
eugenem

ASKER

I've converted DIB to DDB using DIBToDDB function from codeguru. But now something strange happens with some colors: I set transparent color to RGB(150,255,255) but RGB(192,192,192) becames transparent. RGB(1,1,1) converts to RGB(0,0,0), but RGB(255,255,255) or RGB(128,128,128) don't convert.
I think something strange is happening in DIBToDDB.
I think this problem relates to the palette. Do you pass the correct palette handle to TransparentBlt?
Avatar of eugenem

ASKER

This doesn't matter because I use TransparentBlt with memory dc (that doesn't support palette).
There should be a new function to draw a transparent bitmap turning up at codeguru anytime now that is more efficient when blitting to memory dcs.
Avatar of eugenem

ASKER

What new function? I use one from codeguru. I draw to memory dc to realize double-buffering.
Avatar of eugenem

ASKER

Come on, guys! I need this thing to work.
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eugenem

ASKER

How do I choose transparent color? Is it background of DC?
How do I know if I should use DIB_PAL_COLORS or DIB_RGB_COLORS?
What should be rop parameter? What's with these PS_RESERVED and PC_NOCOLLAPSE (should I make first and last 10 colors PC_NOCOLLAPSE and other zeros when I load the palette)?
Avatar of eugenem

ASKER

May be we can switch to e-mail. It will be faster.
My is mednikov@netvision.net.il.
>How do I choose transparent color? Is it background of DC?
Yes, use SetBkColor to set the background color of DC.

>How do I know if I should use DIB_PAL_COLORS or DIB_RGB_COLORS?
It depends on the DIB. It is the same as the parameter of StretchDIBits. It is usually DIB_RGB_COLORS.

>What should be rop parameter?
It is the same as the parameter of StretchDIBits and BitBlt. It is used only in 16-bit version. I forgot to tell you that:
#define DSa  0x008800C6L
#define DSx  0x00660046L
#define rgbBlack RGB(0,0,0)
#define rgbWhite RGB(255,255,255)

>What's with these PS_RESERVED and PC_NOCOLLAPSE (should I make first and last 10 colors PC_NOCOLLAPSE and other zeros when I load the palette)?
Normally, you need to do nothing.

>May be we can switch to e-mail. It will be faster.
Sorry, I prefer using Experts Exchange so that we can keep all the comments here.

Avatar of eugenem

ASKER

Are DSa SRCAND and DSx SRCPAINT?
I need to use this function with memory dc so should I provide palette as a parameter?
What is RGBQ? Same as RGB?
>Are DSa SRCAND and DSx SRCPAINT?
Actually, DSa is the same as SRCAND and DSx is the same as SRCINVERT.

>I need to use this function with memory dc so should I provide palette as a parameter?
You should select the palette of the DIB into the DC and realize it before calling this function. You should always do so when you handle a DIB.

>What is RGBQ? Same as RGB?
#define RGBQ(rgb)   RGB(GetBValue(rgb), GetGValue(rgb), GetRValue(rgb))
Avatar of eugenem

ASKER

How can I realize palette in memoryDC if it doesn't support palettes?
If it doesn't, don't do it.
Avatar of eugenem

ASKER

But is it true that memory dc doesn't support palette?
It depends. When you create the memory DC by using CDC::CreateCompatibleDC, it is compatible with the system display. You can always select and realize a palette no matter whether it supports or not. If it does not, simply no effect.
Avatar of eugenem

ASKER

It works great. Just need to replace
        for (i=0; i < (int)biRgb.bih.biClrUsed; i++)
        {
            if (prgb[i] == RGBQ(rgbBk))
                biRgb.argb[i] = RGBQ(rgbWhite);
            else
                biRgb.argb[i] = RGBQ(rgbBlack);
        }
to
      int size = 1 << lpbi->bmiHeader.biBitCount;

        for (i=0; i < size; i++)
        {
            if (prgb[i] == RGBQ(rgbBk) )
                biRgb.argb[i] = RGBQ(rgbWhite);
            else
                biRgb.argb[i] = RGBQ(rgbBlack);
        }
Thanks a lot!
Avatar of eugenem

ASKER

It works great. Just need to replace
        for (i=0; i < (int)biRgb.bih.biClrUsed; i++)
        {
            if (prgb[i] == RGBQ(rgbBk))
                biRgb.argb[i] = RGBQ(rgbWhite);
            else
                biRgb.argb[i] = RGBQ(rgbBlack);
        }
to
      int size = 1 << lpbi->bmiHeader.biBitCount;

        for (i=0; i < size; i++)
        {
            if (prgb[i] == RGBQ(rgbBk) )
                biRgb.argb[i] = RGBQ(rgbWhite);
            else
                biRgb.argb[i] = RGBQ(rgbBlack);
        }
Thanks a lot!