The principle of transparent Bitmap in MFC

Source: Internet
Author: User
Tags transparent color

In short, is constantly repeatedly masking "filter out" Transparent color on the line, first to the entire code, followed by detailed introduction.

void Ctransdlg::onpaint () {CPAINTDC DC (this); CMEMDC m_dc;if (!M_DC) m_dc. LoadBitmap (IDB_BITMAP4); CMEMDC dcimage (100,100,&DC); CBitmap Bmpmask;bmpmask.createbitmap (100,100,1,1,null); CDC Dcmask;dcmask.createcompatibledc (&DC);d cmask.selectobject (bmpmask);d Cimage.bitblt (0,0,100,100,&m_dc , 0,0,srccopy);d c.         BitBlt (10,10,100,100,&m_dc,0,0,srccopy); Display Figure 1DC.      BitBlt (120,10,100,100,&dcmask,0,0,srccopy); Display Figure 2dcimage.setbkcolor (RGB (255,0,0));d Cmask.bitblt (0,0,100,100,&dcimage,0,0,srccopy);d C.      BitBlt (230,10,100,100,&dcmask,0,0,srccopy); Display Figure 3dc.      BitBlt (340,10,100,100,&dcimage,0,0,srccopy); Display Figure 4dcimage.setbkcolor (RGB (0,0,0));d Cimage.settextcolor (RGB (255,255,255));d C. BitBlt (450,10,100,100,&dcimage,0,0,srccopy);//Display Figure 5dcimage.bitblt (0,0,100,100,&dcmask,0,0,srcand);d C. BitBlt (10,120,100,100,&dcimage,0,0,srccopy);//Display Figure 6DC. BitBlt (120,120,100,100,&dcmask,0,0,srccopy);//Display Figure 7DC. SetBkColor (RGB (255,255,255));d C. BitBlt (230,120,100,100,&DCMASk,0,0,srcand);//Display Figure 8DC. BitBlt (230,120,100,100,&dcimage,0,0,srcpaint);//Display Figure 8}
M_dc. LoadBitmap (IDB_BITMAP4);     Loading the original, is the following, from the red, green, yellow composition, White is the background, not the original image

CMEMDC dcimage (100,100,&DC);//The demonstration principle of a total of three dc,dc to understand as a canvas, Dcimage is a temporary DC, can also be understood as a temporary canvas of 100x100, the original image is not so large, So the background will be revealed;
CBitmap Bmpmask;
CBitmap Bmpmask;bmpmask.createbitmap (100,100,1,1,null);    Create a monochrome bitmap, which you want to see, must be drawn on the canvas, which is copied to the CDC Dcmask;dcmask.createcompatibledc (&DC) on the (BITBLT) DC;           Create a mask with the canvas dcmask.selectobject (bmpmask);             Dcimage.bitblt the monochrome picture on the canvas (0,0,100,100,&m_dc,0,0,srccopy);   M_DC the original copy on this canvas to the temporary canvas dcimage <pre name= "code" class= "CPP" >DC. BitBlt (10,10,100,100,&m_dc,0,0,srccopy);         Displays the original DC on the final canvas DC. BitBlt (120,10,100,100,&dcmask,0,0,srccopy);      Displays a monochrome mask bitmap on the final canvas DC

Dcimage.setbkcolor (RGB (255,0,0));              Set the red color to dcimage the background color of the canvas, which is transparent dcmask.bitblt (0,0,100,100,&dcimage,0,0,srccopy); Dcimage copy himself to Dcmask, tell him red is the background color, dcmask can only show two colors, so dcimage on the red Place White, the other place is the black DC. BitBlt (230,10,100,100,&dcmask,0,0,srccopy);      Show Dcmask this canvas dc.bitblt (340,10,100,100,&dcimage,0,0,srccopy);      Show Dcimage This canvas


Dcimage.setbkcolor (RGB (0,0,0));              Dcimage set Black to Background color Dcimage.settextcolor (RGB (255,255,255));d C. BitBlt (450,10,100,100,&dcimage,0,0,srccopy);//Display Dcimage

Dcimage.bitblt (0,0,100,100,&dcmask,0,0,srcand),//dcimage and Dcmask, note that the transparent color of dcimage is black at this time, So the black in the Dcmask to the dcimage on the Transparent Color dc.bitblt (10,120,100,100,&dcimage,0,0,srccopy);//Display Dcimagedc.bitblt ( 120,120,100,100,&dcmask,0,0,srccopy);//Show Dcmask the  first is the original Dcimage, the second is Dcmask, and the third is the later Dcimage


dc. SetBkColor (RGB (255,255,255)); <span style= "White-space:pre" ></span>//the transparent color of the final canvas DC is set to white DC. BitBlt (230,120,100,100,&dcmask,0,0,srcand);//dcmask with the final canvas and Dc.bitblt (230,120,100,100,&dcimage,0,0, Srcpaint);//dcimage with the final canvas or the   first one is Dcmask, the second is Dcimage, and the third is the final effect.




The principle of transparent Bitmap in MFC

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.