Display transparent bitmap correctly

Source: Internet
Author: User

Display transparent bitmap correctly

 

There are already a lot of documents on the Internet about Transparent Display of bitmaps, but these articles have a very large defect. If a bitmap is not a 24-Bit Bitmap, in other words, when a bitmap is a two-or four-or eight-Bit Bitmap, because these bitmaps use the color palette information, the general Transparent Display bitmap method will cause color disorder, A transparent bitmap cannot be correctly displayed.

Based on the previous documents, I made some corrections to their display methods to correctly display transparent bitmaps of any digits. The following drawtransparentbmp function is a complete function that correctly displays transparent bitmaps. Its parameters are described as follows:
HDC: Target HDC, which is to be drawn as a bitmap
Hbitmap hbmp: the bitmap handle. This bitmap can be a two-, four-, eight-, 16-, and 24-Bit Bitmap.
Rect & rect: area to be drawn
Colorref colortrans: transparent color

The function list is as follows:
Bool drawtransparentbmp (HDC, hbitmap hbmp, rect & rect, colorref colortrans)
{
HDC dcimage, dctrans, dcimage24;
Hbitmap holdbmp24, hbmp24;
Hbitmap holdbmp;
Hbitmap hbmptrans, holdbmptrans;

// Create a memory DC
Dcimage = createcompatibledc (HDC );
Dctrans = createcompatibledc (HDC );
Dcimage24 = createcompatibledc (HDC );

If (dcimage = NULL | dctrans = NULL | dcimage24 = NULL)
// Error: can't create compatible DC
Return false;
// Obtain image attributes
Bitmap BMP;
GetObject (hbmp, sizeof (BMP), & BMP );

// Select the image to dcimage
Holdbmp = (hbitmap) SelectObject (dcimage, hbmp );

// Create a 24-color bitmap to display the transparent bitmap correctly.
Lpbitmapinfo lpbmpinfo;
 
Lpbmpinfo = (bitmapinfo *) New byte [sizeof (bitmapinfoheader)];
 
Lpbmpinfo-> bmiheader. bisize = sizeof (bitmapinfoheader );
Lpbmpinfo-> bmiheader. biplanes = 1;
Lpbmpinfo-> bmiheader. bibitcount = nbitcount;
Lpbmpinfo-> bmiheader. bicompression = bi_rgb;
Lpbmpinfo-> bmiheader. bisizeimage = 0;
Lpbmpinfo-> bmiheader. biclrused = 0;
 
Lpbmpinfo-> bmiheader. biwidth = BMP. bmwidth;
Lpbmpinfo-> bmiheader. biheight = BMP. bmheight;
 
HDC Dc = createcompatibledc (null );
 
// Create a new image
Lpvoid lpbits;
Hbmp24 =: createdibsection (DC, lpbmpinfo, dib_rgb_colors,
& Lpbits, null, 0 );
 
Deletedc (DC );
Delete lpbmpinfo;

If (hbmp24 = NULL)
// Error
Return false;

Holdbmp24 = (hbitmap) SelectObject (dcimage24, hbmp24 );

// Draw the original image into a 24-color bitmap
Bitblt (dcimage24, 0, 0, BMP. bmwidth, BMP. bmheight, dcimage, 0, 0, srccopy );

// Create a mask bitmap
Hbmptrans = createbitmap (BMP. bmwidth, BMP. bmheight, 1, 1, null );
If (hbmptrans = NULL)
// Error
Return false;

// Select the mask bitmap to dctrans.
Holdbmptrans = (hbitmap) SelectObject (dctrans, hbmptrans );
 
// Create a mask image (based on the specified color)
Colorref oldbkcolor = setbkcolor (dcimage24, colortrans );
Bitblt (dctrans, 0, 0, BMP. bmwidth, BMP. bmheight, dcimage24, 0, 0, srccopy );
 
Setbkcolor (dcimage24, RGB (0, 0 ));
Colorref oldtextcolor = settextcolor (dcimage24, RGB (255,255,255 ));
Bitblt (dcimage24, 0, 0, BMP. bmwidth, BMP. bmheight, dctrans, 0, 0, srcand );

// Remove the specified color
Colorref croldback, croldtext;
Croldback = setbkcolor (HDC, RGB (255,255,255 ));
Croldtext = settextcolor (HDC, RGB (0, 0, 0 ));

// Display transparent bitmap
Stretchblt (HDC, rect. Left, rect. Top, rect. Right-rect. Left, rect. Bottom-rect. Top,
Dctrans, 0, 0, BMP. bmwidth, BMP. bmheight, srcand );
Stretchblt (HDC, rect. Left, rect. Top, rect. Right-rect. Left, rect. Bottom-rect. Top,
Dcimage24, 0, 0, BMP. bmwidth, BMP. bmheight, srcpaint );
 
// Restore settings and release resources
SelectObject (dcimage, holdbmp );
 
SelectObject (dcimage24, holdbmp24 );
SelectObject (dctrans, holdbmptrans );

Deleteobject (hbmp24 );
Deleteobject (hbmptrans );

Setbkcolor (HDC, croldback );
Settextcolor (HDC, croldtext );
 
Setbkcolor (dcimage24, oldbkcolor );
Settextcolor (dcimage24, oldtextcolor );

Deletedc (dcimage );
Deletedc (dcimage24 );
Deletedc (dctrans );

Return true;
}

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.