Display of transparent bitmap

Source: Internet
Author: User
Tags transparent color

 

There are many ways to draw a bitmap that contains transparent colors. The simplest method is to call a ready-made function:TransparentBlt can also implement functions similar to TransparentBlt through its own code. The implementation process also has two forms: one is to make a mask bitmap beforehand, and the other is to dynamically generate a mask bitmap. This article describes how to dynamically generate a mask bitmap to draw a transparent area bitmap.

I. Use of the TransparentBlt Function

The TransparentBlt function is run in Windows98/Windows2000 or later versions. The system must contain Msimg32.dll, which can be linked to Msimg32.lib for use.
TransparentBlt in Windows 98 may cause resource leakage. Therefore, we do not recommend that you use this function in 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 start point
  
Int nYOriginSrc,// Source Y start point
  
Int nWidthSrc,// Source width
  
Int nHeightSrc,// Source height
  
UINT crTransparent// Transparent color, COLORREF type
  
);
  

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 (, 0xff ));
  
ImageDC. SelectObject (pOldImageBMP );
  

II. ImplementationTransparentBlt Function

To understand the process of drawing a transparent bitmap, we will build an experimental function with the same TransparentBlt function. It is called TransparentBlt2.

Experiment material: There are two bitmap: bk.bmp is the background image, football.bmp contains the transparent area, the transparent color is blue RGB (, 0xff)
Objective: To use bk.bmp as the background and draw football.bmp into the background to form the following final result.



2.1 Principle of transparent bitmap
Hypothetical football.bmp-> load HBITMAP hImageBMP-> select HDC hImageDC

2.1.1 generate a monochrome mask bitmap for football. The Transparent area is white (all 1) and the non-transparent area is black (all 0)

HBITMAP hMaskBMP = CreateBitmap (nWidthDest, nHeightDest, 1, 1, NULL); // create a 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 of the football bitmap becomes white in the mask bitmap, and the other areas are black.HMaskBMP:
(Figure 1)

2.1.2 set the background color to black and the foreground color to white. The mask Bitmap (figure 1) is the same as the football bitmap "and"

SetBkColor (hImageDC, RGB (0, 0 ));
  
SetTextColor (hImageDC, RGB (1, 255,255,255 ));
  
BitBlt (hImageDC, 0, 0, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND );
  
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.