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: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]
解决方案
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[^].