C + + Set Transparent background image

Source: Internet
Author: User
Tags transparent color

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.