Set transparent background images

Source: Internet
Author: User
Tags transparent color

Background:
There are two images, one is the target background image and the other is a color image with its own background color.
First, draw the color image to the target background image, which can be achieved through BITBLT. However, the effect after implementation is: The color image on the target image has its own background.
The problem arises. How can we remove the background of the color image?
 
Solution:
API function: TransparentBlt this function draws images from the original DC to the Target DC, and sets the transparent color of the original image on the target image.
 
[Cpp]
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 with the background color, the background color of the final color image is eliminated after this function is used.
Example:
[Cpp]
CDC * pDC = GetDC ();
 
CBitmap bmp;
Bmp. LoadBitmap (IDB_BITMAP1 );
 
Bitmap bmp Info;
Bmp. GetObject (sizeof (BITMAP), & bmp info );
 
 
CDC ImageDC;
ImageDC. CreateCompatibleDC (pDC );
 
CBitmap * pOldImageBmp = ImageDC. SelectObject (& bmp );
 
 
CBitmap bmp bk;
BMP bk. LoadBitmap (IDB_BITMAP2 );
 
Bitmap bmp bkinfo;
BMP bk. GetObject (sizeof (BITMAP), & BMP bkinfo );
 
CDC bkDC;
BkDC. CreateCompatibleDC (pDC );
 
BkDC. SelectObject (& bmp bk );
 
TransparentBlt (bkDC. m_hDC, 100,150, BMP info. bmWidth, BMP info. bmHeight, ImageDC. m_hDC, 0, 0, BMP info. bmWidth, BMP info. bmHeight, RGB (, 0); // set the red color to transparent.
 
BitBlt (pDC-> m_hDC, BMP bkinfo. bmWidth, BMP bkinfo. bmHeight, bkDC. m_hDC, SRCCOPY); // draw to the screen

 
 
Principle: Implemented by setting a mask bitmap
1) first create a mask bitmap
2) apply a mask bitmap to the color source image to obtain a new variant image (the transparent color is black, and the other areas are primary colors)
3) use the mask bitmap and the target background image phase (transparent area is transparent, other areas are black)
4) use the new variant image to match the target background image or to obtain the final image.
 
The legend is as follows:
 

 
Remove the background color of the image above to obtain the final image.
 
The steps are as follows:
 
 

 
 
 
 

 
 
 
Code:
[Cpp]
Void TransparentBlt2 (HDC hdcDest, // Target DC
Int nXOriginDest, // target X offset
Int nYOriginDest, // target Y offset
Int nWidthDest, // target width
Int nHeightDest, // target height
HDC hdcSrc, // source DC
Int nXOriginSrc, // source X start point
Int nYOriginSrc, // source Y start point
Int nWidthSrc, // Source width
Int nHeightSrc, // source height
UINT crTransparent // transparent color, COLORREF type
)
{
HBITMAP hOldImageBMP, hImageBMP = CreateCompatibleBitmap (hdcDest, nWidthDest, nHeightDest); // create compatible bitmap
HBITMAP hOldMaskBMP, hMaskBMP = CreateBitmap (nWidthDest, nHeightDest, 1, 1, NULL); // create a monochrome mask bitmap
HDC hImageDC = CreateCompatibleDC (hdcDest );
HDC hMaskDC = CreateCompatibleDC (hdcDest );
HOldImageBMP = (HBITMAP) SelectObject (hImageDC, hImageBMP );
HOldMaskBMP = (HBITMAP) SelectObject (hMaskDC, hMaskBMP );
 
// Copy the bitmap from the source DC to the temporary DC.
If (nWidthDest = nWidthSrc & nHeightDest = nHeightSrc)
BitBlt (hImageDC, 0, 0, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY );
Else
StretchBlt (hImageDC, 0, 0, nWidthDest, nHeightDest,
HdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY );
 
// Set the transparent color
SetBkColor (hImageDC, crTransparent );
 
// Generate a mask bitmap where the transparent area is white and other areas are black.
BitBlt (hMaskDC, 0, 0, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCCOPY );
 
// Generate a bitmap where the transparent area is black and other areas remain unchanged
SetBkColor (hImageDC, RGB (0, 0 ));
SetTextColor (hImageDC, RGB (1, 255,255,255 ));
BitBlt (hImageDC, 0, 0, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND );
 
// The transparent part keeps the screen unchanged, and the other part turns black.
SetBkColor (hdcDest, RGB (1, 255,255,255 ));
SetTextColor (hdcDest, RGB (0, 0 ));
 
 
BitBlt (hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND );
 
// "Or" operation to generate the final result
BitBlt (hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCPAINT );
 
// Clear and restore www.2cto.com
SelectObject (hImageDC, hOldImageBMP );
DeleteDC (hImageDC );
SelectObject (hMaskDC, hOldMaskBMP );
DeleteDC (hMaskDC );
DeleteObject (hImageBMP );
DeleteObject (hMaskBMP );
}


Author: shuilan0066

Related Article

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.