![]() |
The MFC CImage and CDC classes always draw
"square" bitmap images.
![]()
Figure.1 No Background
The white area must be filled with the background image. The figure below shows a "mask" that specifies the area that is not under the circular figure.
![]()
Figure.2 Masked
Here shows the addtion of images. While the color black is represented as RGB(0,0,0), any logical OR operation to the pixels will add it. The white area, where the pixel values are RGB(0xff,0xff,0xff), any logical OR operation to the area will erase the image.
![]()
Figure.3 OR'ed
Now Figure.1 must be added to the white area. To draw images in the white area RGB(0xff,0xff,0xff), use AND operation where added pixel values will not change the value. Adding Figure.1 and Figure.3 will produce the following image.
(Click to enlarge)
Figure.4 AND'ed to the white area
The folloing code shows how to create a mask over an image. The code
first create a CImage instance, fill the background, white out an oval
area (Figure.2), and finally OR's the mask area with the image
(Figure.3). In the code, the image is specified as m_pImage.
int maskWidth=100;
int maskHeight=100;
CRect maskRect(0,0,maskWidth,maskHeight);
CDC *pImageDC=CDC::FromHandle(m_pImage->GetDC());
CImage mask;
VERIFY(mask.Create(maskWidth,maskHeight,m_pImage->GetBPP()));
CDC *pMaskDC=CDC::FromHandle(mask.GetDC());
CBrush brush(RGB(0,0,0));
pMaskDC->FillRect(maskRect,&brush);
CBrush fillBrush(RGB(0xff,0xff,0xff));
pMaskDC->SelectObject(&fillBrush);
pMaskDC->Ellipse(maskRect);
pMaskDC->BitBlt(0,0,maskWidth,maskHeight,pImageDC,0,0,SRCPAINT);
mask.ReleaseDC();
m_pImage->ReleaseDC();