C + + Set Transparent background picture _c language

Source: Internet
Author: User

Background:

There are two pictures, one is the target background picture, the other is a color picture with its own background color

The color picture is first drawn to the target background image, which can be achieved by BitBlt. But the effect is: the target picture, painted up color pictures with its own background.

The problem is, we want to remove the background color picture itself, how should we solve?

Workaround:

Using API functions: TransparentBlt This function draws a picture from the original DC to the target DC and sets the original shape's transparent color on the target graphic.

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,   //Hei Ght of source Rectangle 
 UINT crtransparent//color to make transparent 
);

In this example, when the transparent color is set to a color graphic with its background color, the resulting color of the colored graphic on the final shape 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,100,150,bmpinfo.bmwidth,bmpinfo.bmheight,imagedc.m_hdc,0,0,bmpinfo.bmwidth, Bmpinfo.bmheight,rgb (255,0,0)); Set red to Transparent color 
 
BitBlt (pdc->m_hdc,0,0,bmpbkinfo.bmwidth,bmpbkinfo.bmheight,bkdc.m_hdc,0,0,srccopy);//Draw to screen

Principle: By setting the mask bitmap to implement

1 Create mask Bitmap first
2 Using a mask bitmap to the color of the original image, get a new map (transparent color is black, the other area is the primary color)
3 Use mask bitmap and target background image (transparent area is transparent, other area is black)
4 Use the new variant and the target background image or, get the final diagram

The above mentioned is the entire content of this article, I hope you can enjoy.

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.