Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below code draws a transparent bitmap perfectly but when i change size of the bitmap on screen, transparency is gone. This code is only drawing original bitmap size(80 X 80) as transparent but not able draw transparency properly when i resize(90 X 90) bitmap on screen. Can anybody pin point the problem in the code. Thank you in advance for help.
C++
void CPictureManager::TransparentBitmap(CDC* pDC,CBitmap *pBitmap, CRect render,int iSrcWidth,int iSrcHeight,COLORREF clrpTransColor)
{
	CDC dcTemp;
	dcTemp.CreateCompatibleDC(pDC);
	CBitmap* bmTemp = dcTemp.SelectObject(pBitmap);
	BITMAP bm;
	pBitmap->GetBitmap(&bm);
	POINT ptSize;
	ptSize.x = bm.bmWidth;		// Get width of bitmap
	ptSize.y = bm.bmHeight;		// Get height of bitmap
	dcTemp.DPtoLP(&ptSize, 1);	// Convert from device to logical points

	// Create some DCs to hold temporary data.
	CDC dcMem;
	dcMem.CreateCompatibleDC(pDC);
	CDC dcBack;
	dcBack.CreateCompatibleDC(pDC);
	CDC dcObject;
	dcObject.CreateCompatibleDC(pDC);
	CDC dcSave;
	dcSave.CreateCompatibleDC(pDC);

	// Monochrome DC
	CBitmap bmAndBack;
	bmAndBack.CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);

	// Monochrome DC
	CBitmap bmAndObject;
	bmAndObject.CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);

	// Color DCs
	CBitmap bmAndMem;
	bmAndMem.CreateCompatibleBitmap(pDC,ptSize.x,ptSize.y);
	CBitmap bmSave;
	bmSave.CreateCompatibleBitmap(pDC,ptSize.x,ptSize.y);

	// Each DC must select a bitmap object to store pixel data.
	CBitmap* bmBackOld = (CBitmap*)dcBack.SelectObject(&bmAndBack);
	CBitmap* bmObjectOld = (CBitmap*)dcObject.SelectObject(&bmAndObject);
	CBitmap* bmMemOld = (CBitmap*)dcMem.SelectObject(&bmAndMem);
	CBitmap* bmSaveOld = (CBitmap*)dcSave.SelectObject(&bmSave);

	// Set proper mapping mode.
	dcTemp.SetMapMode(pDC->GetMapMode());

	// Save the bitmap sent here, because it will be overwritten.
	dcSave.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCCOPY);

	// Set the background color of the source DC to the color.
	// contained in the parts of the bitmap that should be transparent
	COLORREF cColor;
	cColor = dcTemp.SetBkColor(clrpTransColor);

	// Create the object mask for the bitmap by performing a BitBlt
	// from the source bitmap to a monochrome bitmap.
	dcObject.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCCOPY);

	// Set the background color of the source DC back to the original
	// color.
	dcTemp.SetBkColor(cColor);

	// Create the inverse of the object mask.
	dcBack.BitBlt(0,0,ptSize.x,ptSize.y,&dcObject,0,0,NOTSRCCOPY);

	// Copy the background of the main DC to the destination.
	dcMem.BitBlt(0,0,ptSize.x,ptSize.y,pDC,render.left,render.top,SRCCOPY);

	// Mask out the places where the bitmap will be placed.
	dcMem.BitBlt(0,0,ptSize.x,ptSize.y,&dcObject,0,0,SRCAND);

	// Mask out the transparent colored pixels on the bitmap.
	dcTemp.BitBlt(0,0,ptSize.x,ptSize.y,&dcBack,0,0,SRCAND);

	// XOR the bitmap with the background on the destination DC.
	dcMem.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCPAINT);

	// Copy the destination to the screen.
	pDC->StretchBlt(render.left,render.top,render.Width(),render.Height(),&dcMem,0,0,iSrcWidth,iSrcHeight,SRCCOPY);

	// Restore the original bitmap.
	dcTemp.BitBlt(0,0,ptSize.x,ptSize.y,&dcSave,0,0,SRCCOPY);

	// Delete the memory bitmaps.
	dcBack.SelectObject(bmBackOld);
	dcObject.SelectObject(bmObjectOld);
	dcMem.SelectObject(bmMemOld);
	dcSave.SelectObject(bmSaveOld);
	dcTemp.SelectObject(bmTemp);
}
Posted
Updated 9-Jul-13 4:51am
v3

1 solution

When stretching your bitmap the color value of your transparency marker color will be slightly changed due to the pixel interpolation process. And hence your transparency color will not be recognized in your display code any longer.

It is somewhat difficult to prevent that from happening. My first try would be to first create an RGBA bitmap that has a true alpha channel and then stretch that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900