C++ sets the transparent background image
- 2020-04-02 03:06:13
- OfStack
Background:
There are two pictures, one is the target background picture, the other is a color picture with its own background color
The color image is first drawn to the target background image, which can be achieved by BITBLT.
But the result is that on the target image, the painted color image has its own background.
So the question is, how do we get rid of the background of the color picture itself?
Solutions:
Use API functions: TransparentBlt This function draws the image in the original DC to the target DC and sets the transparent color of the original graph on the target graph.
BOOL TransparentBlt(
HDC hdcDest, // handle to destination DC
int nXOriginDest, // x-coord of destination upper-left corner
int nYOriginDest, // y-coord of destination upper-left corner
int nWidthDest, // width of destination rectangle
int hHeightDest, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXOriginSrc, // x-coord of source upper-left corner
int nYOriginSrc, // y-coord of source upper-left corner
int nWidthSrc, // width of source rectangle
int nHeightSrc, // height of source rectangle
UINT crTransparent // color to make transparent
);
In this example, when the transparent color is set to the color image's own background color, the color image's own background color on the final image is eliminated after using this function.
CDC* pDC=GetDC();
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
BITMAP bmpInfo;
bmp.GetObject(sizeof(BITMAP),&bmpInfo);
CDC ImageDC;
ImageDC.CreateCompatibleDC(pDC);
CBitmap *pOldImageBmp=ImageDC.SelectObject(&bmp);
CBitmap bmpBK;
bmpBK.LoadBitmap(IDB_BITMAP2);
BITMAP bmpBkInfo;
bmpBK.GetObject(sizeof(BITMAP),&bmpBkInfo);
CDC bkDC;
bkDC.CreateCompatibleDC(pDC);
bkDC.SelectObject(&bmpBK);
TransparentBlt(bkDC.m_hDC,100,150,bmpInfo.bmWidth,bmpInfo.bmHeight,ImageDC.m_hDC,0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,RGB(255,0,0)); //Set red to transparent
BitBlt(pDC->m_hDC,0,0,bmpBkInfo.bmWidth,bmpBkInfo.bmHeight,bkDC.m_hDC,0,0,SRCCOPY); //Draw it on the screen
Principle: by setting the mask bitmap to achieve
1) first, create a mask bitmap
2) apply the mask bitmap to the original color map to obtain the new variation map (the transparent color is black, and other areas are primary colors)
3) use mask bitmap to match the target background image (transparent area is transparent color, other areas are black)
4) use the new variation diagram with the target background diagram phase or
And get the final graph
The above is all the content of this article, I hope you can enjoy it.