Show Author transparent bitmap: Wang Jun
Download This example code contains a plot of transparent color bitmaps. There are a variety of, the easiest way is to call out-of-the-art: TransparentBLT, you can also implement the functions similar to transparentblt through your own code, and there are two forms, one It is a mask bitmap in advance, and the other is a dynamic generating mask bitmap. This article describes the method of dynamically generating mask bitmaps to draw a transparent zone bitmap. First, the use of the TransparentBLT function runs in Windows98 / Windows2000 or higher, and the system is required to include MSIMG32.DLL, and you can link MSIMG32.LIB when used. The TransparentBLT under Windows 98 will generate resource leaks, so it is not recommended to use this function under Win98. The TransparentBLT function prototype is as follows:
Bool TransparentBlt (HDC HDCDEST, / / Target DC INT NXORIGINDEST, / / Target X Offset INT NYORIGINDEST, / / Target Y Offset INT NWIDTHDEST, / / Target Width INT HHEIGHTDEST, / / Target Height HDC HDCSRC, // Source DC INT NxORIGINSRC, // Source X Starting INT NYORIGINSRC, / Source Y Starting INT NWIDTHSRC, // Source Width INT NHEIGHTSRC, // Source Height Uint Crtransparent / / Transparent Color, ColorRef Type); Use Example:
CBitmap FootballBMP; FootballBMP.LoadBitmap (IDB_FOOTBALLBMP); CDC ImageDC; ImageDC.CreateCompatibleDC (pDC); CBitmap * pOldImageBMP = ImageDC.SelectObject (& FootballBMP); TransparentBlt (pDC-> m_hDC, 0, 0, 218, 199, ImageDC.m_hDC, 0, 0, 218, 199, RGB (0, 0, 0xFF)); imagedc.selectObject (PoldImageBMP);
Second, implement the TransparentBLT function
In order to understand the drawing process with a transparent color bitmap, we will create an experiment function with the transparentblt function, called TransparentBLT2.
Experimental material: There are two bitmaps: bk.bmp is a background bitmap, football.bmp contains transparent area, transparent color is blue RGB (0, 0, 0xff)
Experimental purpose: with bk.bmp as the background, football.bmp is drawn into the background to form the following final rendering.
2.1 transparent bit drawing principle
Suppose football.bmp -> load hbitmap himagebmp -> Selected HDC Himagedc
2.1.1 Monochrome mask bitmap of generating football, transparent area is white (all 1), non-transparent area is black (all 0)
Hbitmap hmaskbmp = createbitmap (nwidthdest, nHEightDest, 1, 1, null); // Establish a monochrome bitmap setBkcolor (HimageDC, RGB (0, 0, 0xFF)); // Setting the background color to blue Bitblt (hmaskdc, 0 , 0, nwidthdest, nheightdest, himagedc, 0, 0, srcopy; // Copy to hmaskdc This blue area in the football bit map is in the mask bitmap, and other areas are black. At this time, hmaskbmp is shown below:
(Figure 1)
2.1.2 Setting the background color to black, the foreground color is white, the mask bitmap (Figure 1) and the football bit graphic phase "and" setBkcolor (Himagedc, RGB (0, 0)); setTextColor (himagedc, rgb) (255, 255, 255); Bitblt (Himagedc, 0, 0, NWidthDest, NHEIGHTDEST, HMASKDC, 0, 0, Srcand); this, the area of the mask bitmap in the background color (black) is retained in HimageBMP, foreground color (white The portion of the portion is changed to black. At this time, HimageBMP is shown below:
(Figure II)
2.1.3 Setting the background color is white, the foreground color is black, and the mask bitmap (Figure 1) "and" operation
SetBKColor (HDCDest, RGB (255, 255)); setTextColor (HDCDest, RGB (0)); Bitblt (HDCDest, Nxorigindest, Nhemigindest, NWIDTHDEST, NHEightDest, Hmaskdc, 0, 0, srcand); Mask in white area (Data and 1 Phase "and" Results are constant) keep the background unchanged, the black area is black, and the background is shown below:
(Figure 3)
2.1.4 Wiping HIMAGEBMP (Figure 2) with the background (Figure 3) "or" operation
Bitblt (HDCDEST, NXORIGINDEST, NYORIGINDEST, NWIDTHDEST, NHEIGHTDEST, HIMAGEDC, 0, 0, SRCPAINT); this will draw football to the background.
2.2 TransparentBLT2 functions all implement code
Void TransparentBLT2 (HDC HDCDEST, / / Target DC INT NXORIGINDEST, / / Target x Offset INT NYORIGINDEST, / / Target Y Offset INT NWIDTHDEST, / / Target Width INT NHEIGHTDEST, / / Target High HDC HDCSRC, // Source DC Int nXOriginSrc, // starting point source X int nYOriginSrc, // source Y a starting point int nWidthSrc, // source width int nHeightSrc, // UINT crTransparent // source highly transparent color, COLORREF type) {HBITMAP hOldImageBMP, hImageBMP = CreateCompatibleBitmap (hdcDest, nWidthDest , nHeightDest); // create compatibility 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); // DC source bitmap are copied to a 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); // Setting Ming color setbkcolor (himagedc, crtransparent); // generates a transparent area for white, other area for black mask bitmap bitblt (hmaskdc, 0, 0, nwidthdest, nHEightDest, himagedc, 0, 0, srcopy); // generation The transparent area is black, and the other region remains unchanged. SetBKColor (Himagedc, RGB (0)); SetTextColor (Himagedc, RGB (255, 255)); Bitblt (Himagedc, 0, 0, NWIDTHDEST, NHEIGHTDEST, HMASKDC , 0, 0, srcand); // Transparent part keeps the screen unchanged, other parts turn into black setBkcolor (HDCDest, RGB (255, 255)); SetTextColor (HDCDest, RGB (0, 0)); Bitblt (HDCDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND); // "or" operation to produce the final effect BitBlt (hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCPAINT); // Clean up, restore SelectObject (HimageDC, HoldImageBMP);
Deletedc (HimageDC); SelectObject (Hmaskdc, HoldmaskBMP); deletedc (hmaskdc); deleteObject (HimageBMP); DELETEOBJECT (HMASKBMP); another version of TransparentBLT: Transparentbltu
TransparentBLTU is a function of Christian Graus published in WINDEV, the function is consistent with TransparentBLT, the following is all implementation code:
bool TransparentBltU (HDC dcDest, // handle to Dest 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 nHeightDest, // height of destination rectangle HDC dcSrc, // 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) {if (nWidthDest <1) return false; if (nWidthSrc <1) return false; if (nHeightDest <1) return false; if (nHeightSrc <1) return false; HDC dc = CreateCompatibleDC (NULL); HBITMAP bitmap = CreateBitmap (nWidthSrc, nHeightSrc, 1, GetDeviceCaps (dc, BITSPIXEL), NULL); if (bitmap == NULL) {DeleteDC (dc) Return False;} hbitmap oldbitmap = (hbitmap) SelectObject (DC, Bitmap); if (! Bitblt (DC, 0, 0, NWIDTHSRC, NHEIGHTSRC, DCSRC, NXORIIN) Src, nYOriginSrc, SRCCOPY)) {SelectObject (dc, oldBitmap); DeleteObject (bitmap); DeleteDC (dc); return false;} HDC maskDC = CreateCompatibleDC (NULL); HBITMAP maskBitmap = CreateBitmap (nWidthSrc, nHeightSrc, 1, 1, NULL); if (maskBitmap == NULL) {SelectObject (dc, oldBitmap); DeleteObject (bitmap); DeleteDC (dc); DeleteDC (maskDC); return false;} HBITMAP oldMask = (HBITMAP) SelectObject (maskDC, maskBitmap); SetBKColor (Maskdc, RGB (0, 0)); SetTextColor (MaskDC, RGB (255, 255)); if (! Bitblt (Maskdc, 0,0, NWIDTHSRC, NHEIGHTSRC, NULL, 0, 0, blackness) {SelectObject (MaskDC, Oldmask; deleteObject (maskbitmap); deletedc (Maskdc); SelectObject (DC, OldBitmap); deleteObject (Bitmap); deletedc (dc); return false;
SetBkcolor (DC, Crtransparent); Bitblt (MaskDC, 0, 0, NWIDTHSRC, NHEIGHTSRC, DC, 0, 0, SRCINVERT); SetBKColor (DC, RGB (0, 0)); SetTextColor (DC, RGB (255, 255, 255) )); BitBlt (dc, 0,0, nWidthSrc, nHeightSrc, maskDC, 0,0, SRCAND); HDC newMaskDC = CreateCompatibleDC (NULL); HBITMAP newMask; newMask = CreateBitmap (nWidthDest, nHeightDest, 1, GetDeviceCaps (newMaskDC, BITSPIXEL ), NULL); if (newMask == NULL) {SelectObject (dc, oldBitmap); DeleteDC (dc); SelectObject (maskDC, oldMask); DeleteDC (maskDC); DeleteDC (newMaskDC); DeleteObject (bitmap); DeleteObject (maskBitmap ); return false;} SetStretchBltMode (newMaskDC, COLORONCOLOR); HBITMAP oldNewMask = (HBITMAP) SelectObject (newMaskDC, newMask); StretchBlt (newMaskDC, 0, 0, nWidthDest, nHeightDest, maskDC, 0, 0, nWidthSrc, nHeightSrc, SRCCOPY) ; SelectObject (maskDC, oldMask); DeleteDC (maskDC); DeleteObject (maskBitmap); HDC newImageDC = CreateCompatibleDC (NULL); HBITMAP newImage = CreateBitmap (nWidthDest, nHeightDest, 1, GetDeviceCaps (newMaskDC, BITSPIXEL), NULL); if (NewImage == NULL) {SelectObject (dc, oldBitmap); DeleteDC (dc); DeleteDC (newMaskDC); DeleteObject (bitmap); return false;} HBITMAP oldNewImage = (HBITMAP) SelectObject (newImageDC, newImage); StretchBlt (newImageDC, 0, 0, nWidthDest, nHeightDest, dc, 0, 0, nWidthSrc, nHeightSrc, SRCCOPY); SelectObject (dc, oldBitmap); DeleteDC (dc); DeleteObject (bitmap); BitBlt (dcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, newMaskDC, 0, 0, SRCAND); BitBlt (dcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, newImageDC, 0, 0, SRCPAINT); SelectObject (newImageDC, oldNewImage); DeleteDC (newImageDC); SelectObject (newMaskDC, oldNewMask); DeleteDC (newmaskdc); deleteObject (newimage); deleteObject (newmask);
Return True;} Description: The TransparentBLT2 function provided herein is intended to illustrate the display principle of the transparent bitmap. It is recommended to use the ready-made TransparentBLT function to draw a transparent bitmap in the Windows2000 environment.
Related Resources: Talking about the display of transparent bitmaps in VC