Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all:-)

I have the following question:

I have two CBitMap objects, which represent 2 .png-Files.

Now i want to overlay them the following way, where the 2nd cbitmap is over the first Bitmap:

if a pixel of the 2nd cbitmap is white, i want to display the pixel of the first cbitmap. if it is not white, i want to display the pixel of the 2nd cbitmap.


Is this possible to achieve?


I tried it with the Alphablend() function, where i set the alpha-value of the white pixels of the 2nd cbitmap to zero.

Always, only the second png is displayed...

Any ideas?

Thanks for any help!

[edit]
Here is my code, for future visitors:

C++
void PngImage::DrawOverlayedBitmap(CBitmap* pBitmapUnder, CBitmap* pBitmapUpper, CWnd* pWnd, CPoint Offset)
{
	// get bitmap information
	BITMAP bmpInfo;
	pBitmapUnder->GetObject(sizeof(bmpInfo), &bmpInfo);

	// get size
	CSize size;
	size.cx = bmpInfo.bmWidth;
	size.cy = bmpInfo.bmHeight;

	// get window's client device context
	CClientDC* pDC = new CClientDC(pWnd);

	// create memory device context
	CDC* memDC = new CDC; memDC->CreateCompatibleDC(pDC);

	// buffer bitmap
	CBitmap* old = memDC->SelectObject(pBitmapUnder);

	// copy bitmap bits
	pDC->BitBlt
	(
		Offset.x,		// specifies the x-coordinate (in logical units) of the upper-left corner of the destination rectangle
		Offset.y,		// specifies the y-coordinate (in logical units) of the upper-left corner of the destination rectangle
		size.cx,		// specifies the width (in logical units) of the destination rectangle
		size.cy,		// specifies the height (in logical units) of the destination rectangle
		memDC,			// specifies the source device context
		0,				// specifies the x-coordinate (in logical units) of the upper-left corner of the source rectangle
		0,				// specifies the x-coordinate (in logical units) of the upper-left corner of the source rectangle
		SRCCOPY			// specifies the raster operation to be performed
	);

	// reselect first bitmap
	memDC->SelectObject(pBitmapUpper);

	// copy bitmap bits
	pDC->BitBlt
	(
		Offset.x,		// specifies the x-coordinate (in logical units) of the upper-left corner of the destination rectangle
		Offset.y,		// specifies the y-coordinate (in logical units) of the upper-left corner of the destination rectangle
		size.cx,		// specifies the width (in logical units) of the destination rectangle
		size.cy,		// specifies the height (in logical units) of the destination rectangle
		memDC,			// specifies the source device context
		0,				// specifies the x-coordinate (in logical units) of the upper-left corner of the source rectangle
		0,				// specifies the x-coordinate (in logical units) of the upper-left corner of the source rectangle
		SRCAND		// specifies the raster operation to be performed
	);

	// delete device context and reset pointer
	delete memDC; memDC = 0;

	// delete device context and reset pointer
	delete pDC; pDC = 0;
}


[/edit]
Posted
Updated 18-Feb-15 23:20pm
v2

1 solution

You should load your first image into a DC then load the second into a memory DC, and then blit the second one into the first, using the appropriate operation code as described in https://msdn.microsoft.com/en-us/library/windows/desktop/dd183370%28v=vs.85%29.aspx[^].
 
Share this answer
 
v2
Comments
Member 10781325 19-Feb-15 4:52am    
thx very much!! that does it:-)
Member 10781325 19-Feb-15 5:17am    
Moved to the original question.
Member 10781325 19-Feb-15 5:18am    
i know, its ugly, thats just test code;-)
Richard MacCutchan 19-Feb-15 5:21am    
Thanks for posting, will maybe help someone else in the future.

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