Originally posted by: Paco Garcia
void CYourView::DrawTransparentBitmap(CDC *pDC, CBitmap *Bitmap, short xStart, short yStart, int nWidth, int nHeight, COLORREF cTransparentColor)
// Create two memory dcs for the image and the mask
// Select the image into the appropriate dc
// Create the mask bitmap
// Select the mask bitmap into the appropriate dc
// Build mask based on transparent colour
// Do the work - True Mask method - cool if not actual display
// Restore settings
[ccode]
This is great code. If you need to be able to resize the bitmap here is a variation
{
COLORREF crOldBack = pDC->SetBkColor(0x00FFFFFF);
COLORREF crOldText = pDC->SetTextColor(0x00000000);
CDC dcImage, dcTrans;
dcImage.CreateCompatibleDC(pDC);
dcTrans.CreateCompatibleDC(pDC);
CBitmap* pOldBitmapImage = dcImage.SelectObject(Bitmap);
CBitmap bitmapTrans;
BITMAP bm;
Bitmap->GetBitmap(&bm);
int bmpWidth = bm.bmWidth;
int bmpHeight = bm.bmHeight;
bitmapTrans.CreateBitmap(bmpWidth, bmpHeight, 1, 1, NULL);
CBitmap* pOldBitmapTrans = dcTrans.SelectObject(&bitmapTrans);
dcImage.SetBkColor(cTransparentColor);
dcTrans.BitBlt(0, 0, bmpWidth, bmpHeight, &dcImage, 0, 0, SRCCOPY);
pDC->StretchBlt(xStart, yStart, nWidth, nHeight, &dcImage, 0, 0, bmpWidth, bmpHeight, SRCINVERT);
pDC->StretchBlt(xStart, yStart, nWidth, nHeight, &dcTrans, 0, 0, bmpWidth, bmpHeight, SRCAND);
pDC->StretchBlt(xStart, yStart, nWidth, nHeight, &dcImage, 0, 0, bmpWidth, bmpHeight, SRCINVERT);
dcImage.SelectObject(pOldBitmapImage);
dcTrans.SelectObject(pOldBitmapTrans);
pDC->SetBkColor(crOldBack);
pDC->SetTextColor(crOldText);
}
[/ccode]
Originally posted by: Prashant Verma
I am building an application that uses bitmaps. I want the application to take the default windows color toning. for this I am making the bitmaps transparent, so that it can take the windows face-color. This approach works fine when the bitmaps are over the dialog frame. but i have bitmaps that are out of the dialog frame just to make the appearence as a tab page. The problem exists here. The bitmaps that i am using as a tab, when made transparent, takes the windows background color. But i need to give them the face color. How do i go for this problem. Any more clarifications are appriciated. please send the solution ASAP...
Originally posted by: DongJiang
It is very good for me,thankyou.
but, why i can not print bitmatp correctly?
i can display bitmatp correctly.
Originally posted by: Zhang Kun
It does not work, if I set display color to True Color(32 bit). But works on 256 and 16 bit colors.
Who can tell me why?
Originally posted by: Stoj
tried using the transparent bitmap code, but i cant get it to work. Has anyone got any example programs where it works, i just need to see how to use the code.
ReplyOriginally posted by: Mizan Rahman
I need to know how to make a bitmap look like it is disabled.
It can be seen in CommandButton of VB.
If a picture is assign to the button and style is set to Graphyc and Enabled is set to False.
If any one has any idea or sample please e-mail it to me.
ReplyOriginally posted by: Vovat
Bm_Sprite.LoadBitmap(IDB_TRANSPARENT_BITMAP); // must be in resource for current example
dc_Sprite.CreateCompatibleDC(&dc); // two additional DC for M- and I- bitmaps
Bm_Frame.CreateBitmap(BmW,BmH,1,1,NULL); // M- bitmap creation
oBm_Sprite=dc_Sprite.SelectObject(&Bm_Sprite); // here we put M- and I-bitmap on appropriate DC
COLORREF tColor=dc_Sprite.GetPixel(XColor,YColor); // Transparency color definition
COLORREF Bg=dc_Sprite.SetBkColor(tColor); // here we draw M-bitmap
/////////////////////////// transparent drawing /////////////////////////////////
dc.BitBlt(X,Y,BmW,BmH,&dc_Frame,0,0,SRCAND);
/////////////////////////////////////////////////////////////////////////////////
dc_Frame.SelectObject(oBm_Frame); // here we restore previos state of both additional DC
//
// ...Very simple example of transparent bitmap drawing...
// Just put it in OnPaint and check how it works
//
CPaintDC dc(this); // standard part of OnPaint function
int BmW=200, BmH=200; // bitmap size definition
int X=0,Y=0; // transparent bitmap position on dc (see above)
int XColor=0, YColor=0; // we define transparentcy color as color of pixel (0x0)
// in IDB_TRANCPARENT_BITMAP bitmap
CDC dc_Sprite,
dc_Frame;
CBitmap Bm_Sprite, * oBm_Sprite,
Bm_Frame, * oBm_Frame;
dc_Frame.CreateCompatibleDC(&dc); // are created here
oBm_Frame=dc_Frame.SelectObject(&Bm_Frame); // ...
dc_Frame.BitBlt(0,0,BmW,BmH,&dc_Sprite,0,0,SRCCOPY); // ...
dc_Sprite.SetBkColor(Bg); // ...
dc_Frame.BitBlt(0,0,BmW,BmH,NULL,0,0,DSTINVERT); // here we draw I-bitmap
dc_Sprite.BitBlt(0,0,BmW,BmH,&dc_Frame,0,0,SRCAND); // ...
dc_Frame.BitBlt(0,0,BmW,BmH,NULL,0,0,DSTINVERT); // ...
dc.BitBlt(X,Y,BmW,BmH,&dc_Sprite,0,0,SRCPAINT);
dc_Sprite.SelectObject(oBm_Sprite); // ...
Originally posted by: Jason S. Clary
Does anyone know of a way to get a DC handle or some other method of capturing an bitmap from an area just below the current window? Getting notifications of changes would be helpfull, as well. I'm looking at doing true alpha channel transparencies.
I have toyed withe the idea of getting the full screen handle and hiding my window temporarily and grabbing the chunk below it and rendering the new masked image and redisplaying my window but the flickering would be unbearable. And I have now idea how to watch for changes in the window directly below mine.
Actually, I thought about navigating the zorder and getting the window below mine but it gets tricky when there are overlapped windows below mine. If it weren't for the overlapped window problem, I'm guessing I could even capture paint events for the windows below mine even though they may be in a different application.
So, does anyone have an easier way than running through the complete zorder looking for topmost windows below mine that draw an area under mine and watching for their paint events and capturing what I need from them? I'm thinking this will be fairly CPU intensive since I'd need to rescan the zorder frequently.
ReplyOriginally posted by: Dan Nissenbaum
It uses MFC's DC wrappers to perform a transparent blt of the bitmap onto the background. The bitmap needs to be white where you want it to be transparent when blitted to the destination (screen).
I modified Egon Lubber's code to create this code.
CPaintDC dc(this);
CBitmap * pOldBitmap = dcBitmap.SelectObject(&m_bitmap);
CDC maskdc;
maskdc.BitBlt(0, 0, m_ptSizeBitmap.x, m_ptSizeBitmap.y, &dcBitmap, 0, 0, SRCCOPY);
// Note: at this point, maskdc contains our mask: a black-and-white image mask of m_bitmap
// do a double blt operation (first with mask, then with bitmap)
dcBitmap.SetBkColor(oldColor);
The following code is from my CDialog::OnPaint handler; it assumes
you have a CBitmap class member (already initialized with a bitmap resource) called m_bitmap.
CDC dcBitmap;
dcBitmap.CreateCompatibleDC(&dc);
COLORREF oldColor = dcBitmap.SetBkColor(COLORREF(0xFFFFFFFF));
COLORREF oldDCColor = dc.SetBkColor(COLORREF(0xFFFFFFFF));
// set bkclr to white; we'll need it later
maskdc.CreateCompatibleDC(NULL); // automatically creates a 1x1 monochrome bitmap,
// which is the default for a memory DC when first created
// Note: this is a memory DC since the HDC param is NULL
/****************/
//first the logo*/
CBitmap ddb;
ddb.CreateCompatibleBitmap(&maskdc, m_ptSizeBitmap.x, m_ptSizeBitmap.y);
// creates an (empty) full-size bitmap with a color format compatible with the
// color format of the memory DC, which is monochrome
CBitmap * pOldMemBitmap = maskdc.SelectObject(&ddb);
// select the (as of yet empty but full-sized) monochrome
// bitmap into the memory device context.
// Note: the bitmap (monochrome) is compatible with the
// memory DC (also monochrome), as it needs to be
// NOTE: all the magic occurs in the above fn.
// As quoted from the VC++ documentation for CDC::BitBlt:
//
// "If destination, source, and pattern bitmaps do not have the same color format,
// the BitBlt function converts the source and pattern bitmaps to match the destination.
// The foreground and background colors of the destination bitmap are used in the conversion.
// [...]
// To convert color to monochrome, BitBlt sets pixels that match the background color to white
// and sets all other pixels to black. BitBlt uses the foreground and background colors of
// the color device context to convert from color to monochrome."
//
// In our case, the background color for both source and destination is white, so all
// non-white pixels map to black in the mask.
// Our bitmaps need to have white background for this to work.
dc.BitBlt(5,5, m_ptSizeBitmap.x, m_ptSizeBitmap.y, &maskdc, 0, 0, MERGEPAINT);
dc.BitBlt(5,5, m_ptSizeBitmap.x, m_ptSizeBitmap.y, &dcBitmap, 0, 0, SRCAND);
// clean up the (temporary) DC's... good programming practice
dcBitmap.SelectObject(&pOldBitmap);
maskdc.SelectObject(pOldMemBitmap);
dc.SetBkColor(oldDCColor);
CDialog::OnPaint();
Originally posted by: Gunasekaran
Printing the Transparent bitmap in Postscript Printer gives unexpected output.
Can any one Suggest