Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Transparet Color in BITMAP

276 views
Skip to first unread message

mkag

unread,
Nov 23, 2001, 10:43:07 PM11/23/01
to
I have a HBITMAP, and I want to change some color of bitmap to Transparent
color. for e.g. I want to convert all pixel of RGB(255,0,255) to
transparent. How I can do this?

Jeff Partch

unread,
Nov 23, 2001, 11:45:01 PM11/23/01
to
Hi!

"mkag" <manish...@hotmail.com> wrote in message news:eknqJoJdBHA.1848@tkmsftngp05...


> I have a HBITMAP, and I want to change some color of bitmap to Transparent
> color. for e.g. I want to convert all pixel of RGB(255,0,255) to
> transparent. How I can do this?

You could take a look at MSDN Article Q79212, "HOWTO: Drawing Transparent Bitmaps", and see if
something there helps you with this. Personally, I would wimp-out and and stuff the HBITMAP and
COLORREF into an imagelist and draw it using the ILD_TRANSPARENT style.

HTH,

Jeff...
--
Please post all follow-ups to the newsgroup only.

Linto Mathew

unread,
Nov 23, 2001, 11:46:05 PM11/23/01
to
See the link
 
 
Thanks & Regards
 
Linto Mathew

Daniel Bevan

unread,
Nov 24, 2001, 1:18:42 AM11/24/01
to
Bitmaps do not have transparent colours.

AN EASY WAY:
some devices support transparent BitBlt(..) opperations.
This example from the MSDN Library shows how you can check for and use this
capability:

// transfer non-gray pixels to the screen
if (GetDeviceCaps(hdc, CAPS1) & C1_TRANSPARENT)
{
SetBkMode(hdc, NEWTRANSPARENT);
SetBkColor(hdc, RGB(0xc0,0xc0,0xc0));
BitBlt(hdc, x, y, cx, cy, hdcButton, 0, 0, SRCCOPY);
}
else
{
... // old mask and blt code
}

THE REAL WAY:
The way these transparent drawing functions usualy work is to use a second
bitmap of the same size as a mask containing the transparent area.
The mask is combined with the destination area to remove or modify the
colour and then the bitmap is combined with that.

A simple mask technique is to use a monochrome mask bitmap,AND the mask with
the destination then OR the bitmap with the masked destination.
All black pixels of the mask are opaque. If the source bitmap has black
where the mask is white it will be completely transparent, if not the
resulting colour will be (destination colour OR source colour).

To implement a transparent colour bitmap painter this way you would you
would
-set up a bitmap for the mask
-loop through each pixel of the bitmap
-if the pixel colour == transparent colour
-set pixel balck
-set mask pixel white
-else
set mask pixel black
-end loop
-AND mask with destination
-OR bitmap with destination

TAKING IT A STEP FURTHER:
The bitmap doesn't have to be completely transparent either, the mask can
contain any colours although a grey scale mask is easiest to work with for
transparencies, the whiter the pixel the more transparent.
This is the idea behind 'alpha blending' the mask is a fourth colour showing
_how_ transparent a pixel is.
In this case the transparent source pixels should not be black but should be
"faded" to the proper level before combining them with the masked
destination, pixels that are dominant in destination must be dim in the
source.
This just requires one more step, the procedure is:

AND mask with destination
AND (NOT mask) with source
OR masked source with masked destination.

If you need more information on how to do these bitmap combinations see:
BitBlt(...), CDC (or device context, HDC), GetPixel(...), SetPixel(...)

OTHER MASK COMBINATIONS:
Icons use a different combination method for different results.
The mask is monochrome, the mask is combined with the destination using AND,
then the image is combined to the masked area using XOR.

An XOR opperation inverts any destination bits that correspond to TRUE bits
in the source so

for monochrome images
S M Result
0 0 Black
0 1 Screen colour - transparent
1 0 White
1 1 Inverse Screen - Inverse tranparent

for 24 bot colour:

Destination XOR 0 = Destination //transparent area
Destination XOR 0xFFFFFF = Inverse Destination //commonly used in cursors
0 XOR Source = Source //opaque area
0xFFFFFF XOR Source = Inverse Source

and several million other combinations for colour images

Welcome to the wonderful world of bit masking.
Daniel Bevan

"mkag" <manish...@hotmail.com> wrote in message
news:eknqJoJdBHA.1848@tkmsftngp05...

Joseph M. Newcomer

unread,
Nov 25, 2001, 1:52:25 PM11/25/01
to
You've had several good answers, but here's another approach: what do you mean by
"transparent"? That is, do you mean "if I draw it on a dialog I get the background color
showing through" or do you mean "if I draw it on top of another bitmap I get all the other
colors showing through"? The latter question has already been answered. However, if all
you want is the background color, use LoadImage with the LR_LOADTRANSPARENT option, which
replaces all pixels of the same color as the (0,0) pixel (top left corner) with the
COLOR_WINDOW, or LR_MAP3DCOLORS which will properly handle colors like the dialog color,
even if the user changes the color scheme. Note that these techniques only work on
256-color and coarser images; they don't apply to 24-bit, 16-bit, or 15-bit color images.
In these cases, the complex solution is, alas, the only available one.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

mkag

unread,
Nov 25, 2001, 9:39:50 PM11/25/01
to
Hi Joseph
Actually I have a CImageList, And I want to draw images after loading these
from image list on different buttons, Menu, dialogs

hIcon = m_ImageList.ExtractIcon(i);
GetIconInfo(hIcon, &iconinfo);
CBitmap* pBitmap;
pBitmap = new CBitmap;
pBitmap->Attach(iconinfo.hbmColor);

Now I have a CBitmap object, I want to draw this bitmap on buttons, menus
etc. but I want that a particular color of image replace by background
color.
Say all Magenta color Pixel become Transparent.

"Joseph M. Newcomer" <newc...@flounder.com> wrote in message
news:d4f20u4phpn7vdbcr...@4ax.com...

Joseph M. Newcomer

unread,
Nov 26, 2001, 12:01:07 AM11/26/01
to
That's the easier option, because you can create the bitmaps with the standard 3D colors
and then LoadImage with the LR_MAP3DCOLORS option to get them to conform to the current
scheme, since buttons, menus, and such use the same color scheme.
joe

mkag

unread,
Nov 26, 2001, 1:29:33 AM11/26/01
to

But what about my question, how I change the color of some pixels in this
bitmap by transparent color?

"Joseph M. Newcomer" <newc...@flounder.com> wrote in message

news:23j30u0f5atss0kq9...@4ax.com...

Joseph M. Newcomer

unread,
Nov 27, 2001, 1:52:26 AM11/27/01
to
You can't. Read the earlier answers. You have to create two bitmaps, one of which is a
masking bitmap, based on the selected pixel color, and one of which is the bitmap other
than the pixel color. You then AND the mask to the underlying bitmap to remove the pixels
you want to draw, then OR the pixels in to the resulting gap. There is no concept of
"trransparency" for bitmaps using standard GDI functions. What I was suggesting was that
for certain restricted cases of "transparency", there are simpler solutions.
joe

Daniel Bevan

unread,
Nov 27, 2001, 10:56:23 AM11/27/01
to
CImageLists take all of the hard work out of bitmap transparency.

The answer to your problem is to use the function
int CImageList::Add(CBitmap* pbmImage, COLORREF crMask );

The following is from the MSDN:
----------------------------------------------------------
CImageList::Add
int Add( CBitmap* pbmImage, CBitmap* pbmMask );

int Add( CBitmap* pbmImage, COLORREF crMask );

int Add( HICON hIcon );

Return Value

Zero-based index of the first new image if successful; otherwise - 1.

Parameters

pbmImage Pointer to the bitmap containing the image or images. The number
of images is inferred from the width of the bitmap.

pbmMask Pointer to the bitmap containing the mask. If no mask is used with
the image list, this parameter is ignored.

crMask Color used to generate the mask. Each pixel of this color in the
given bitmap is changed to black and the corresponding bit in the mask is
set to one.

hIcon Handle of the icon that contains the bitmap and mask for the new
image.

Remarks

Call this function to add one or more images or an icon to an image list.

------END MSDN------

You can either make your own mask and use the first function or specify the
transparent colour and let it generate the mask, image pair from the image
you supply.

The third function doesn't need a mask or transparent colour as that
information is part of the icon.

You should also set the CImageList background colour to CLR_NONE eg if the
colour you want transparent in your bitmap is red:

myimagelist.SetBkColor(CLR_NONE);

myimagelist.Add(&picture1, 0x0000FF);

myimagelist.Draw( &drawDC, 0, drawPoint, ILD_NORMAL );

This should be what you are looking for,
Daniel Bevan

"mkag" <manish...@hotmail.com> wrote in message

news:#J5mHOidBHA.1880@tkmsftngp07...

Ralf Buschmann

unread,
Nov 27, 2001, 2:24:09 PM11/27/01
to
On Wed, 28 Nov 2001 02:26:23 +1030, "Daniel Bevan"
<dwb...@ozemail.com.au> wrote:

>CImageLists take all of the hard work out of bitmap transparency.

Remember, however, that ImageList supports transparency only for bitmaps
with up to 256 colors.

Ralf.

Daniel Bevan

unread,
Nov 28, 2001, 8:39:21 AM11/28/01
to
Thanks for that tip, I was unaware of that limitation.

Daniel Bevan

"Ralf Buschmann" <busc...@ibherzog.de> wrote in message
news:BKRM7.73$sd3....@monolith.news.easynet.net...

Ralf Buschmann

unread,
Nov 28, 2001, 12:55:55 PM11/28/01
to
On Thu, 29 Nov 2001 00:09:21 +1030, "Daniel Bevan"
<dwb...@ozemail.com.au> wrote:

>> Remember, however, that ImageList supports transparency only for bitmaps
>> with up to 256 colors.

>Thanks for that tip, I was unaware of that limitation.

Sorry, should have been more specific. It is the ImageList_AddMasked()
function that accepts only bitmaps with up to 256 colors. I haven't
tried it, but you should be able to use ImageList_Add() for bitmaps with
more bpp, but then you have to supply the mask bitmap yourself.

Ralf.

0 new messages