Correct display transparent bitmap

zhaozj2021-02-11  172

Correct display transparent bitmap

There is already a lot about the documentation of the transparent display bitmap, but there is a very big defect in these articles. If a bitmap is not a bitmap of 24-bit color, in other words, when a bitmap When 2 or 4 or 8-bit bitmaps, since these bitmaps use the palette information, use the general transparent display bitmap method, the phenomenon of color miscarriage will not correctly display a transparent bit. Fig.

On the basis of the former person, I made some corrections for their display methods, and the transparent bitmap of any number of digits can be displayed correctly. The following DrawTransParentBMP function is a completely displayed transparent bitmap function, and each parameter is as follows: HDC HDC: Target HDC, the HDCHBITMAP HBMP: bitmap of the bitmap, this bitmap can be 2, 4-bit, 8-bit, 16-bit, 24-bit bitmap. Rect & Rect: The area that will be drawn Colorref ColorTrans: Transparent Color

Function list is as follows: BOOL DrawTransparentBmp (HDC 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 = CREATECOMPALEDC (HDC); DCIMAGE24 = CREATECOMPALDC (HDC);

IF (DCIMAGE == NULL || DCTRANS == NULL || DCIMAGE24 == NULL) // Error: can't create compatible DC Return false; // Get image attribute Bitmap BMP; getObject (HBMP, SIZEOF (BMP), & BMP );

// Select the picture to DCIMAGE HoldBMP = (HbitMap) SelectObject (DCImage, HBMP);

// Create a 24 color bitmap so that the bitmap can be displayed with a transparent color LPBITMAPINFO lpBmpInfo correctly; 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 picture 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 picture to the 24-color bitmap Bitblt (DCIMAGE24, 0, 0, BMP.BMWIDTH, BMP.BMHEIGHT, DCIMAGE, 0, 0, SRCCOPY);

// Create MASK bitmap HBMPTRANS = CREATEBITMAP (bmp.bmwidth, bmp.bmheight, 1, 1, null); if (hbmptrans == null) // Error Return False;

// Select 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,0)); COLORREF oldtextcolor = SetTextColor (dcImage24, RGB (255,255,255)); BitBlt (dcImage24, 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));

// Show transparent bitmap strets, RECT.TOP, RECT.RIGHT - Rect.Top, Rect.Bottom - Rect.top, DCTRANS, 0, 0, BMP.BMWIDTH, BMP.BMHEIGHT, SRCAND Stretchblt (HDC, Rect.Lect, Rect.top, Rect.right - Rect.Top, Rect.bottom - Rect.top, DCImage24, 0, 0, Bmp.BmWidth, Bmp.BmHeight, Srcpaint); // Restore Setup 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;}

A Wen http://tulipstudio.yeah.net

转载请注明原文地址:https://www.9cbs.com/read-5818.html

New Post(0)