Draw a translucent bitmap and how to draw transparent bitmaps

xiaoxiao2021-03-06  14

Draw a translucent bitmap

Have time, we want to show a translucent bitmap. That is to say, we display a bitmap B to the A bitmap, and hope that the A bitmap part of the A bitmap is not all through the B bitmap. For example, a bitmap is a graph, B is a prompt bitmap, we want to see the displayed curve while displaying prompts, but does not require a context of curve, you need to use a translucent bitmap. The curve looks like it is penetrated from the B bitmap. In fact, the translucent technology is an osmotic technology. We can choose a variety of penetration formulas, where we choose (a and 0x7f) or B. Note that white cannot produce penetration.

// Parameter Description: // HDIB - Bitmark Handle // PPAL - Bitmap Tune // XDest - Display Bitmap The upper left corner X coordinate // YDEST - Display bitmap of the upper left corner Y coordinate Void DrawsemitransparentBitMap (CDC * pDC, int nXDest, int nYDest, HGLOBAL hDIB, CPalette * pPal) BITMAPINFO & bmInfo = * (LPBITMAPINFO) hDIB; int nColors = bmInfo.bmiHeader.biClrUsed bmInfo.bmiHeader.biClrUsed:? 1 << bmInfo.bmiHeader.biBitCount; int nWidth = bminfo.bmiheader.biwidth; int nheight = bminfo.bmiheader.biheight;

LPVOID lpDIBBits = (LPVOID) (bmInfo.bmiColors nColors); CDC memDC; memDC.CreateCompatibleDC (pDC); CBitmap bmp; bmp.CreateCompatibleBitmap (pDC, nWidth, nHeight); CBitmap * pOldBitmap = memDC.SelectObject (& bmp); if (pDC-> GetDeviceCaps (RASTERCAPS) & RC_PALETTE && nColors <256) CPalette * pOldMemPalette = memDC.SelectPalette (pPal, FALSE); memDC.RealizePalette (); :: SetDIBitsToDevice (memDC.m_hDC, 0, 0, nWidth, nHeight, 0, 0, 0, NHEIGHT, LPDIBBIB, (LPBitmapInfo) HDIB, DIB_RGB_COLORS;

CDC maskDC; CBitmap mbm; maskDC.CreateCompatibleDC (pDC); mbm.CreateCompatibleBitmap (pDC, nWidth, nHeight); maskDC.SelectObject (& mbm); maskDC.FillSolidRect (CRect (0, 0, nWidth, nHeight), RGB (0x7F, 0x7F, 0x7F); PDC-> Bitblt (NxDest, Nydest, Nwidth, NHEIGHT, & MASKDC, 0, 0, Srcand); PDC-> Bitblt (NXDest, Nydest, NWIDTH, NHEIGHT, & MEMDC, 0, 0, SRCPAINT); MEMDC.SelectObject (PoldbitMap);

How to draw a transparent bitmap

The usual method of painting transparent bitmap is to use masks. The so-called mask is a black and white two-color bitmap. He and the transparent bitmap are corresponding, the mask describes the need to transparent parts in the bitmap, the transparent part is black, but the opaque is white, white Part of the transparent part. Suppose Figure A is the transparent bitmap to be drawn, Figure B is a mask, Figure A is a capital letter A, the letter is red, the background is black, Figure B background is white, there is a black letter A The shape of Figure A is the same. For example, we have to draw a transparent draw in a background of a blue sky and white clouds, just draw only the red letter A. We can first carry out the picture B and the background, and then proceed or operate in the background B and background. The code implemented with the VC MFC is as follows: void cdemodlg :: onpaint () cpaintdc DC (this); cbitmap BmpBack, BMPA, BMPB, * POLDBACK, * POLDA, * POLDB; BMPBACK.LOADBITMAP (IDB_BACKGROUND); // Load Background BMPA.LOADBITMAP (IDB_BITMAPA); // Loading Figure Abmpb.LoadBitmap (IDB_bitmapb); // Loading Figure BCDC DCBACK, DCA, DCB; // Declaration Three Memory DCs are used to draw DCBack.createCompaBLEDC (& DC); DCA. CreateCompaTibleDC (& DC); DCB.createCompatibleDC (& DC); // creates these three memory DCs into PainTDC-compatible DC

PoldBack = DcBack.selectObject (& BmpBack); Polda = DCA.SelectObject (& BMPA); Poldb = DCB.SelectObject (& BMPB); // Put three bitmaps to the corresponding DCDC.bitblt (0, 0, 100, 100, & DCBACK, 0, 0, SRCCOPY); // Draw DC.Bitblt (0, 0, 48, 48, & DCB, 0, 0, SRCAND); // Draw Mask Picture BDC.bitblt (0,48, 48, & DCA, 0, 0, SrcPaint); // Draw the map of ADCBACK.SELECTOBJECT (POLDB); DCBACK.SELECTOBJECT (POLDB); // Remove bitmap from memory DC; You will see the red letter A transparently painted on the background.

Methods with masks must be done in advance, equal to double the resource is equal to more than double consumption, and more wasteful. There is also a method of drawing a transparent bitmap. The basic principle is the same, just do not need to make a mask in advance, the mask is required, but requires a transparent bitmap to specify a transparent color, all this transparent color The place is painted into transparency. The code implemented with the VC MFC is as follows: / * This is a function CDC * PDC for painting transparent bitmaps requires a CDC pointer of a bitmap. Uint IDIMAGE Bitmap Resources IdcRect & Reed PDC Points in PDC COLORREF RGBMASK bitmap Transparent color

* / Void DrawTransparentBitmap (CDC * pDC, UINT IDImage, Crect & rect, COLORREF rgbMask) CDC ImageDC, MaskDC; Cbitmap Image, * pOldImage; Cbitmap maskBitmap, * pOldMaskDCBitmap; Image.LoadBitmap (IDImage); ImageDC.CreateCompatibleDC (pDC); pOldImage = ImageDC.SelectObject (& Image); MaskDC.CreateCompatibleDC (pDC); maskBitmap.CreateBitmap (rect.Width (), rect.Height (), 1, 1, NULL); pOldMaskDCBitmap = MaskDC.SelectObject (& maskBitmap); ImageDC.SetBkColor (RGBMASK); Maskdc.bitblt (0, 0, Rect.width (), Rect.Height (), & imagedc, 0, 0, srcopy; Imagedc.setBkcolor (RGB (0, 0, 0)); Imagedc.SetTextColor (RGB (255, 255, 255); imagedc.bitblt (0, 0, Rect.width (), Rect.height (), & Maskdc, 0, 0, srcand; PDC-> Bitblt (Rect.Left, Rect.top, Rect .Width (), Rect.height (), & maskdc, 0, 0, srcand; PDC-> Bitblt (Rect.LID, Rect.top, Rect.width (), Rect.Height (), & imagedc, 0, 0 , SRCPAINT); MaskDC.SelectObject (pOldMaskDCBitmap); ImageDC.SelectObject (pOldImage); void CDemoDlg :: OnPaint () CPaintDC dc (this); Cbitmap BmpBack, * pOldBack,; BmpBack.LoadBitmap (IDB_BACKGROUND );

CDC dcBack; dcBack.CreateCompatibleDC (& dc); pOldBack = dcBack.SelectObject (& BmpBack); dc.BitBlt (0,0,100,100, & dcBack, 0,0, SRCCOPY); DrawTransparentBitmap (& dc, IDB_BITMAPA, Crect (0,0,48, 48), RGB (192, 192, 0));

DcBack.SelectObject (PoldBack);

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

New Post(0)