"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.
"mkag" <manish...@hotmail.com> wrote in message news:eknqJoJdBHA.1848@tkmsftngp05...
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 [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm
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" <newc...@flounder.com> wrote in message
news:23j30u0f5atss0kq9...@4ax.com...
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...
>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
"Ralf Buschmann" <busc...@ibherzog.de> wrote in message
news:BKRM7.73$sd3....@monolith.news.easynet.net...
>> 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.