How to draw a transparent bitmap

xiaoxiao2021-03-06  14

Excerpt from www.pcvc.net

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 VC MFC is as follows: void cdemodlg :: onpaint () {cpaintdc dc (this); cBitmap BmpBack, BMPA, BMPB, * POLDBACK, * POLDA, * POLDB; BMPBACK.LOADBITMAP (IDB_BACKGROUND); // Load Background Figure BMPA.LOADBITMAP (IDB_bitmapa); // Loading Figure a bmpb.loadbitmap (idb_bitmapb); // Loading Figure B CDC DcBack, DCA, DCB; // Declaration Three Memory DC is used to draw DCBack.createCompatiPLEDC (& DC) ; dcA.CreateCompatibleDC (& dc); dcB.CreateCompatibleDC (& dc); // these three memory DC to create and PaintDC compatible DC pOldBack = dcBack.SelectObject (& BmpBack); pOldA = dcA.SelectObject (& BmpA); pOldB = DCB.SelectObject (& BMPB); // Put three bitmaps in the corresponding DC DC.Bitblt (0, 0, 0, srcopy); // Draw background DC.Bitblt (0, 0, 48, 48, & DCB, 0, 0, srcand; // Painting Mask Diagram B DC.Bitblt (0, 0, 48, 48, & DCA, 0, 0, Srcpaint); // Drawing with or Shouting a dcback.selectObject (PoldBack); DcBack.SelectObject (Polda); DcBack.SelectObject (Poldb); // Remove bitmap from memory DC} You will see red letter A transparently drawn 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.

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

New Post(0)