Transparent Bitmap Display Author: study

Source: Internet
Author: User
Tags transparent color

Display of transparent bitmaps
Author: study

http://www.vckbase.com/document/viewdoc/?id=532

Download the sample code for this article

There are many ways to draw bitmaps that contain transparent colors, the simplest method is to call out-of-the-box functions: TransparentBlt, can also be implemented by their own code similar to the TRANSPARENTBLT function, the implementation process also has two forms, one is to do a mask bitmap beforehand, The other is a dynamically generated mask bitmap. This article describes a method for dynamically generating a mask bitmap to draw a bitmap with a transparent region.

First, the use of TransparentBlt function

The TransparentBlt function runs above windows98/windows2000, the system needs to include Msimg32.dll, and can be linked Msimg32.lib when used.
TransparentBlt under WINDOWS98 generates a resource leak, so it is not recommended to use this function under WIN98.
The TransparentBlt function is prototyped as follows:

BOOL TransparentBlt (HDC hdcdest,      //target dcint nxorigindest,   //target x offset int nyorigindest,   //target y offset int nwidthdest ,     //target width int hheightdest,    //target height hdc hdcsrc,//         source Dcint nxoriginsrc,//    source x start int nyoriginsrc,    // SOURCE y start int nwidthsrc,//      source width int nheightsrc,//     source height uint crtransparent  //Transparent color, COLORREF type);

Examples of Use:

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, realize the TRANSPARENTBLT function

In order to understand the drawing process with transparent color bitmap, we have to build an experimental function consistent with 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)
Objective: To draw the football.bmp into the background with bk.bmp as the background, and to form the following final.



2.1 Principle of Transparent bitmap drawing
Suppose football.bmp, load hbitmap himagebmp, and select HDC Himagedc

2.1.1 Generates a monochrome mask bitmap for soccer, the transparent area is white (full 1), and the opaque area is black (all 0)

Hbitmap hmaskbmp = CreateBitmap (Nwidthdest, Nheightdest, 1, 1, NULL); Establish monochrome bitmap SetBkColor (Himagedc, RGB (0,0,0xff)); Set the background color to blue BitBlt (HMASKDC, 0, 0, nwidthdest, nheightdest, Himagedc, 0, 0, srccopy); Copy to HMASKDC

In this way, the blue area in the soccer bitmap is white in the mask bitmap, and the other area is black, at this time hmaskbmp such as:
(Figure I)

2.1.2 Set the background color to black, the foreground color to white, the mask bitmap (figure I) with the soccer bitmap "with"

SetBkColor (Himagedc, RGB (0,0,0)); SetTextColor (Himagedc, RGB (255,255,255)); BitBlt (HIMAGEDC, 0, 0, nwidthdest, nheightdest, HMASKDC, 0, 0, Srcand);

In this way, the area of the background color (black) in the masked bitmap is preserved in himagebmp, and the portion of the foreground color (white) becomes black. At this time himagebmp such as:
(Figure II)

2.1.3 Set the background color to white, the foreground color to black, and the mask bitmap (figure I) with the background "and" operations

SetBkColor (Hdcdest,rgb (255,255,255)); SetTextColor (Hdcdest,rgb (0,0,0)); BitBlt (Hdcdest, Nxorigindest, Nyorigindest, Nwidthdest, Nheightdest, HMASKDC, 0, 0, Srcand);

The white area in the mask (data with 1-phase "and" results unchanged) keeps the background intact and the black area becomes black, where the background appears as follows:
(Figure III)

2.1.4 the "or" Operation of Himagebmp (figure II) and background (figure III)

BitBlt (Hdcdest, Nxorigindest, Nyorigindest, Nwidthdest, Nheightdest, Himagedc, 0, 0, srcpaint);

This will draw the football to the background.

2.2 TRANSPARENTBLT2 function All implementation code

void TransparentBlt2 (HDC hdcdest,//target dcint nxorigindest,//target x offset int nyorigindest,//target y offset int nwidthdest, Target width int nheightdest,//target height hdc hdcsrc,//Source Dcint nxoriginsrc,//source x start int nyoriginsrc,//source y start int nwi DTHSRC,//source width int nheightsrc,//source height UINT crtransparent//Transparent color, COLORREF type) {hbitmap holdimagebmp, himagebmp = Crea Tecompatiblebitmap (Hdcdest, Nwidthdest, nheightdest);//Create a compatible bitmap hbitmap holdmaskbmp, hmaskbmp = CreateBitmap (nWidthDest , Nheightdest, 1, 1, NULL);//Create a monochrome mask bitmap Hdchimagedc = CreateCompatibleDC (hdcdest); HDCHMASKDC = 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); Elsestretchblt ( Himagedc, 0, 0, nwidthdest, Nheightdest, hDCSrc, Nxoriginsrc, Nyoriginsrc, NWIDTHSRC, nheightSRC, srccopy);//Set Transparent Color SetBkColor (HIMAGEDC, crtransparent);//Generate transparent area is white, other area is black Mask bitmap BitBlt (hmaskdc, 0, 0, Nwidthdest, Nheightdest, Himagedc, 0, 0, srccopy);//Generate a transparent area of black, other areas remain unchanged bitmap SetBkColor (Himagedc, RGB (0,0,0)); SetTextColor (Himagedc, RGB (255,255,255)); BitBlt (HIMAGEDC, 0, 0, nwidthdest, nheightdest, HMASKDC, 0, 0, Srcand);//Transparent part keeps the screen intact, the other part turns black SetBkColor (Hdcdest,rgb ( 255,255,255)); SetTextColor (Hdcdest,rgb (0,0,0)); BitBlt (Hdcdest, Nxorigindest, Nyorigindest, Nwidthdest, Nheightdest, HMASKDC, 0, 0, Srcand);//"or" operation, produces the final effect BitBlt ( Hdcdest, Nxorigindest, Nyorigindest, Nwidthdest, Nheightdest, Himagedc, 0, 0, srcpaint);//clean, restore SelectObject (HImageDC, holdimagebmp);D Eletedc (HIMAGEDC); SelectObject (HMASKDC, holdmaskbmp);D Eletedc (HMASKDC);D eleteobject (himagebmp);D eleteobject (HMaskBMP);}

Another version of

2.3 transparentblt: Transparentbltu

Transparentbltu is Christian Graus A function published in WinDev, functionally consistent with TRANSPARENTBLT, is the 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 Desti      Nation 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 Co Rner int NWIDTHSRC,//width of source rectangle int NHEIGHTSRC,//height of source rectangle UINT C     Rtransparent//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, NXORIGINSRC, Nyorig          INSRC, 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,0));     SetTextColor (MASKDC, RGB (255,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,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, BI     Tspixel), 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, new     Imagedc, 0, 0, srcpaint);     SelectObject (Newimagedc, oldnewimage);     DeleteDC (NEWIMAGEDC);     SelectObject (NEWMASKDC, Oldnewmask);     DeleteDC (NEWMASKDC);        DeleteObject (NewImage);         DeleteObject (Newmask); return true;}


Description: The TRANSPARENTBLT2 function provided in this article is intended to illustrate the display principle of transparent bitmaps, and it is recommended to use out-of-the-box TRANSPARENTBLT functions to draw transparent bitmaps in Windows2000 or above environments.

Http://www.cnblogs.com/witty/archive/2012/05/05/2485222.html

Transparent Bitmap Display Author: study

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.