Background:
There are two pictures, one is the target background picture, and the other is a color picture with its own background color
This color picture is first drawn to the target background image, this step can be achieved by BitBlt. But the effect is: on the target picture, the painted color picture with its own background.
The problem is, we want to remove the background of the color picture itself, how should we solve it?
Workaround:
Use the API function: TransparentBlt This function draws a picture from the original DC to the destination DC, and sets the transparent color of the original shape on the target graph at the same time.
BOOL TransparentBlt (HDC hdcdest,//handle to Destination DC intNxorigindest,//X-coord of destination Upper-left corner intNyorigindest,//Y-coord of destination Upper-left corner intNwidthdest,//width of destination rectangle intHheightdest,//height of destination rectangleHDC hDCSrc,//handle to Source DC intNXORIGINSRC,//X-coord of source Upper-left corner intNYORIGINSRC,//Y-coord of source Upper-left corner intNWIDTHSRC,//width of source rectangle intNHEIGHTSRC,//height of source rectangleUINT crtransparent//color to make transparent);
In this example, when the transparent color is set to a color graphic with a background color, the resulting color on the final graph is eliminated when the function is used.
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, -, Max, BMPINFO.BMWIDTH,BMPINFO.BMHEIGHT,IMAGEDC.M_HDC,0,0, Bmpinfo.bmwidth,bmpinfo.bmheight,rgb (255,0,0));//set red to Transparent colorBitBlt (PDC->M_HDC,0,0, BMPBKINFO.BMWIDTH,BMPBKINFO.BMHEIGHT,BKDC.M_HDC,0,0, srccopy);//draw on the screen
Principle: By setting a mask bitmap to achieve
1) First create a mask bitmap
2) Use Mask Bitmap action on the color original image, to obtain a new variant of the map (transparent color is black, other areas are the primary color)
3) Use mask bitmap with target background (transparent area is transparent, other area is black)
4) Use the new variant and target background to get the final picture
C + + Set Transparent background image