Image processing: mask

Source: Internet
Author: User
Tags transparent color
Http://www.tomore.com/4/3764.html

To draw a transparent bitmap, you can use a mask. The so-called mask is a black and white bitmap. It corresponds to the bitmap to be transparent. The mask describes the parts to be transparent in the bitmap, and the parts to be transparent are black, the opacity is white, and the white part is transparent.

Assume that figure A is a transparent bitmap to be drawn, and Figure B is a mask. Figure A is an uppercase letter A, the letter is red, and the background is black, figure B has a white background, with a black letter A in the same shape as Figure.

For example, if we want to draw a transparently on the background of a blue sky and white clouds, we just need to draw the red letter. We can perform and operate on Figure B and the background first, and then perform or operate on Figure B and the background.

The Code implemented with VC ++ MFC is as follows:
Void cdemodlg: onpaint ()
{
Cpaintdc DC (this );
Cbitmap BMP back, bmpa, bmp B, * poldback, * Polda, * poldb;

BMP back. loadbitmap (idb_background); // load the background image
Bmpa. loadbitmap (idb_bitmapa); // load fig
Bmp B. loadbitmap (idb_bitmapb); // load fig B

CDC dcback, DCA, and DCB; // declare three memory DC for drawing
Dcback. createcompatibledc (& DC );
DCA. createcompatibledc (& DC );
DCB. createcompatibledc (& DC); // create the three memory DC and the paintdc compatible DC

Poldback = dcback. SelectObject (& BMP back );
POldA = dcA. SelectObject (& BmpA );
POldB = dcB. SelectObject (& bmp B); // select three bitmaps into the corresponding DC

Dc. BitBlt (100,100, & dcBack, SRCCOPY); // draw the background
Dc. BitBlt (, 48, 48, & dcB, SRCAND); // use the same method to draw a mask image B
Dc. BitBlt (, 48, & dcA, SRCPAINT); // use an OR method to draw A shadow.

DcBack. SelectObject (pOldBack );
DcBack. SelectObject (pOldA );
DcBack. SelectObject (pOldB); // deletes a bitmap from the memory DC.
}

You will see the red letter A painted transparently on the background.

To use a mask, you must prepare a mask in advance. The size of the mask and the bitmap is equal to or equal to double the resource usage, which is a waste. There is also a way to draw a transparent bitmap, the basic principle is the same, but you do not need to prepare a mask in advance, dynamically generate a mask as needed, but the need for a transparent bitmap must specify a transparent color, wherever this is transparent, it is transparent.

The Code implemented with VC ++ MFC is as follows:
/*
This is a function used to draw a transparent bitmap.
CDC * the CDC pointer for which the pDC needs to draw bitmap
UINT IDImage bitmap resource ID
CRect & rect specifies the position of the bitmap in pDC
Transparent color of COLORREF rgbMask bitmap
*/
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, srccopy );

Imagedc. setbkcolor (#000000 );
Imagedc. settextcolor (# ffffff );
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. left, rect. top, rect. Width (), rect. Height (), & ImageDC, 0, 0, SRCPAINT );

MaskDC. SelectObject (pOldMaskDCBitmap );
ImageDC. SelectObject (pOldImage );
}

Void CDemoDlg: OnPaint ()
{
CPaintDC dc (this );

CBitmap BMP back, * pOldBack ,;
BMP back. LoadBitmap (IDB_BACKGROUND );

CDC dcBack;
DcBack. CreateCompatibleDC (& dc );
POldBack = dcBack. SelectObject (& BMP back );

Dc. BitBlt (100,100, & dcBack, SRCCOPY );
DrawTransparentBitmap (& dc, IDB_BITMAPA, CRect (0, 0, 48), # c0c000 );

DcBack. SelectObject (pOldBack );
}

Http://www.tomore.com/4/3764.html

To draw a transparent bitmap, you can use a mask. The so-called mask is a black and white bitmap. It corresponds to the bitmap to be transparent. The mask describes the parts to be transparent in the bitmap, and the parts to be transparent are black, the opacity is white, and the white part is transparent.

Assume that figure A is a transparent bitmap to be drawn, and Figure B is a mask. Figure A is an uppercase letter A, the letter is red, and the background is black, figure B has a white background, with a black letter A in the same shape as Figure.

For example, if we want to draw a transparently on the background of a blue sky and white clouds, we just need to draw the red letter. We can perform and operate on Figure B and the background first, and then perform or operate on Figure B and the background.

The Code implemented with VC ++ MFC is as follows:
Void cdemodlg: onpaint ()
{
Cpaintdc DC (this );
Cbitmap BMP back, bmpa, bmp B, * poldback, * Polda, * poldb;

BMP back. loadbitmap (idb_background); // load the background image
Bmpa. loadbitmap (idb_bitmapa); // load fig
Bmp B. loadbitmap (idb_bitmapb); // load fig B

CDC dcback, DCA, and DCB; // declare three memory DC for drawing
Dcback. createcompatibledc (& DC );
DCA. createcompatibledc (& DC );
DCB. createcompatibledc (& DC); // create the three memory DC and the paintdc compatible DC

Poldback = dcback. SelectObject (& BMP back );
POldA = dcA. SelectObject (& BmpA );
POldB = dcB. SelectObject (& bmp B); // select three bitmaps into the corresponding DC

Dc. BitBlt (100,100, & dcBack, SRCCOPY); // draw the background
Dc. BitBlt (, 48, 48, & dcB, SRCAND); // use the same method to draw a mask image B
Dc. BitBlt (, 48, & dcA, SRCPAINT); // use an OR method to draw A shadow.

DcBack. SelectObject (pOldBack );
DcBack. SelectObject (pOldA );
DcBack. SelectObject (pOldB); // deletes a bitmap from the memory DC.
}

You will see the red letter A painted transparently on the background.

To use a mask, you must prepare a mask in advance. The size of the mask and the bitmap is equal to or equal to double the resource usage, which is a waste. There is also a way to draw a transparent bitmap, the basic principle is the same, but you do not need to prepare a mask in advance, dynamically generate a mask as needed, but the need for a transparent bitmap must specify a transparent color, wherever this is transparent, it is transparent.

The Code implemented with VC ++ MFC is as follows:
/*
This is a function used to draw a transparent bitmap.
CDC * the CDC pointer for which the pDC needs to draw bitmap
UINT IDImage bitmap resource ID
CRect & rect specifies the position of the bitmap in pDC
Transparent color of COLORREF rgbMask bitmap
*/
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, SRCCOPY );

ImageDC. SetBkColor (#000000 );
ImageDC. SetTextColor (# ffffff );
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. Left, rect. Top, rect. Width (), rect. Height (), & imagedc, 0, 0, srcpaint );

Maskdc. SelectObject (poldmaskdcbitmap );
Imagedc. SelectObject (poldimage );
}

Void cdemodlg: onpaint ()
{
Cpaintdc DC (this );

Cbitmap BMP back, * poldback ,;
BMP back. loadbitmap (idb_background );

CDC dcback;
Dcback. createcompatibledc (& DC );
Poldback = dcback. SelectObject (& BMP back );

DC. bitblt (100,100, & dcback, srccopy );
Drawtransparentbitmap (& DC, idb_bitmapa, crect (0, 0, 48), # c0c000 );

Dcback. SelectObject (poldback );
}

Http://www.tomore.com/4/3764.html

To draw a transparent bitmap, you can use a mask. The so-called mask is a black and white bitmap. It corresponds to the bitmap to be transparent. The mask describes the parts to be transparent in the bitmap, and the parts to be transparent are black, the opacity is white, and the white part is transparent.

Assume that figure A is A transparent bitmap to be drawn, and Figure B is A mask. Figure A is an uppercase letter A, the letter is red, and the background is black, figure B has A white background, with A black letter A in the same shape as Figure.

For example, if we want to draw A transparently on the background of A blue sky and white clouds, we just need to draw the red letter. We can perform and operate on Figure B and the background first, and then perform or operate on Figure B and the background.

The Code implemented with VC ++ MFC is as follows:
Void CDemoDlg: OnPaint ()
{
CPaintDC dc (this );
CBitmap BMP back, BmpA, bmp B, * pOldBack, * pOldA, * pOldB;

BMP back. LoadBitmap (IDB_BACKGROUND); // load the background image
BmpA. LoadBitmap (IDB_BITMAPA); // load fig
Bmp B. LoadBitmap (IDB_BITMAPB); // load fig B

CDC dcBack, dcA, and dcB; // declare three memory DC for drawing
DcBack. CreateCompatibleDC (& dc );
DcA. CreateCompatibleDC (& dc );
DcB. CreateCompatibleDC (& dc); // create the three memory DC and the PaintDC compatible DC

POldBack = dcBack. SelectObject (& BMP back );
POldA = dcA. SelectObject (& BmpA );
POldB = dcB. SelectObject (& bmp B); // select three bitmaps into the corresponding DC

Dc. BitBlt (100,100, & dcBack, SRCCOPY); // draw the background
Dc. BitBlt (, 48, 48, & dcB, SRCAND); // use the same method to draw a mask image B
Dc. BitBlt (, 48, & dcA, SRCPAINT); // use an OR method to draw A shadow.

DcBack. SelectObject (pOldBack );
DcBack. SelectObject (pOldA );
DcBack. SelectObject (pOldB); // deletes a bitmap from the memory DC.
}

You will see the red letter A painted transparently on the background.

To use a mask, you must prepare a mask in advance. The size of the mask and the bitmap is equal to or equal to double the resource usage, which is a waste. There is also a way to draw a transparent bitmap, the basic principle is the same, but you do not need to prepare a mask in advance, dynamically generate a mask as needed, but the need for a transparent bitmap must specify a transparent color, wherever this is transparent, it is transparent.

The Code implemented with VC ++ MFC is as follows:
/*
This is a function used to draw a transparent bitmap.
CDC * the CDC pointer for which the pDC needs to draw bitmap
UINT IDImage bitmap resource ID
CRect & rect specifies the position of the bitmap in pDC
Transparent color of COLORREF rgbMask bitmap
*/
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, SRCCOPY );

ImageDC. SetBkColor (#000000 );
ImageDC. SetTextColor (# ffffff );
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. left, rect. top, rect. Width (), rect. Height (), & ImageDC, 0, 0, SRCPAINT );

MaskDC. SelectObject (pOldMaskDCBitmap );
ImageDC. SelectObject (pOldImage );
}

Void CDemoDlg: OnPaint ()
{
CPaintDC dc (this );

CBitmap BMP back, * pOldBack ,;
BMP back. LoadBitmap (IDB_BACKGROUND );

CDC dcBack;
DcBack. CreateCompatibleDC (& dc );
POldBack = dcBack. SelectObject (& BMP back );

Dc. BitBlt (100,100, & dcBack, SRCCOPY );
DrawTransparentBitmap (& dc, IDB_BITMAPA, CRect (0, 0, 48), # c0c000 );

DcBack. SelectObject (pOldBack );
}

Http://www.tomore.com/4/3764.html

To draw a transparent bitmap, you can use a mask. The so-called mask is a black and white bitmap. It corresponds to the bitmap to be transparent. The mask describes the parts to be transparent in the bitmap, and the parts to be transparent are black, the opacity is white, and the white part is transparent.

Assume that figure A is a transparent bitmap to be drawn, and Figure B is a mask. Figure A is an uppercase letter A, the letter is red, and the background is black, figure B has a white background, with a black letter A in the same shape as Figure.

For example, if we want to draw a transparently on the background of a blue sky and white clouds, we just need to draw the red letter. We can perform and operate on Figure B and the background first, and then perform or operate on Figure B and the background.

The Code implemented with VC ++ MFC is as follows:
Void cdemodlg: onpaint ()
{
Cpaintdc DC (this );
Cbitmap BMP back, bmpa, bmp B, * poldback, * Polda, * poldb;

BMP back. loadbitmap (idb_background); // load the background image
Bmpa. loadbitmap (idb_bitmapa); // load fig
Bmp B. loadbitmap (idb_bitmapb); // load fig B

CDC dcback, DCA, and DCB; // declare three memory DC for drawing
Dcback. createcompatibledc (& DC );
DCA. createcompatibledc (& DC );
DCB. createcompatibledc (& DC); // create the three memory DC and the paintdc compatible DC

Poldback = dcback. SelectObject (& BMP back );
Polda = DCA. SelectObject (& bmpa );
Poldb = DCB. SelectObject (& bmp B); // select three bitmaps into the corresponding DC

DC. bitblt (100,100, & dcback, srccopy); // draw the background
DC. bitblt (, 48, 48, & DCB, srcand); // use the same method to draw a mask image B
DC. bitblt (, 48, & DCA, srcpaint); // use an OR method to draw a shadow.

Dcback. SelectObject (poldback );
Dcback. SelectObject (Polda );
Dcback. SelectObject (poldb); // deletes a bitmap from the memory DC.
}

You will see the red letter a painted transparently on the background.

To use a mask, you must prepare a mask in advance. The size of the mask and the bitmap is equal to or equal to double the resource usage, which is a waste. There is also a way to draw a transparent bitmap, the basic principle is the same, but you do not need to prepare a mask in advance, dynamically generate a mask as needed, but the need for a transparent bitmap must specify a transparent color, wherever this is transparent, it is transparent.

The Code implemented with VC ++ MFC is as follows:
/*
This is a function used to draw a transparent bitmap.
CDC * the CDC pointer for which the PDC needs to draw bitmap
Uint idimage bitmap resource ID
Crect & rect specifies the position of the bitmap in PDC
Transparent color of colorref rgbmask bitmap
*/
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, SRCCOPY );

ImageDC. SetBkColor (#000000 );
ImageDC. SetTextColor (# ffffff );
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. Left, rect. Top, rect. Width (), rect. Height (), & imagedc, 0, 0, srcpaint );

Maskdc. SelectObject (poldmaskdcbitmap );
Imagedc. SelectObject (poldimage );
}

Void cdemodlg: onpaint ()
{
Cpaintdc DC (this );

Cbitmap BMP back, * poldback ,;
BMP back. loadbitmap (idb_background );

CDC dcback;
Dcback. createcompatibledc (& DC );
Poldback = dcback. SelectObject (& BMP back );

DC. bitblt (100,100, & dcback, srccopy );
Drawtransparentbitmap (& DC, idb_bitmapa, crect (0, 0, 48), # c0c000 );

Dcback. SelectObject (poldback );
}

Http://www.tomore.com/4/3764.html

To draw a transparent bitmap, you can use a mask. The so-called mask is a black and white bitmap. It corresponds to the bitmap to be transparent. The mask describes the parts to be transparent in the bitmap, and the parts to be transparent are black, the opacity is white, and the white part is transparent.

Assume that figure A is a transparent bitmap to be drawn, and Figure B is a mask. Figure A is an uppercase letter A, the letter is red, and the background is black, figure B has a white background, with a black letter A in the same shape as Figure.

For example, if we want to draw a transparently on the background of a blue sky and white clouds, we just need to draw the red letter. We can perform and operate on Figure B and the background first, and then perform or operate on Figure B and the background.

The Code implemented with VC ++ MFC is as follows:
Void cdemodlg: onpaint ()
{
Cpaintdc DC (this );
Cbitmap BMP back, bmpa, bmp B, * poldback, * Polda, * poldb;

BMP back. loadbitmap (idb_background); // load the background image
Bmpa. loadbitmap (idb_bitmapa); // load fig
Bmp B. loadbitmap (idb_bitmapb); // load fig B

CDC dcback, DCA, and DCB; // declare three memory DC for drawing
Dcback. createcompatibledc (& DC );
DCA. createcompatibledc (& DC );
DCB. createcompatibledc (& DC); // create the three memory DC and the paintdc compatible DC

Poldback = dcback. SelectObject (& BMP back );
POldA = dcA. SelectObject (& BmpA );
POldB = dcB. SelectObject (& bmp B); // select three bitmaps into the corresponding DC

Dc. BitBlt (100,100, & dcBack, SRCCOPY); // draw the background
Dc. BitBlt (, 48, 48, & dcB, SRCAND); // use the same method to draw a mask image B
Dc. BitBlt (, 48, & dcA, SRCPAINT); // use an OR method to draw A shadow.

DcBack. SelectObject (pOldBack );
DcBack. SelectObject (pOldA );
DcBack. SelectObject (pOldB); // deletes a bitmap from the memory DC.
}

You will see the red letter A painted transparently on the background.

To use a mask, you must prepare a mask in advance. The size of the mask and the bitmap is equal to or equal to double the resource usage, which is a waste. There is also a way to draw a transparent bitmap, the basic principle is the same, but you do not need to prepare a mask in advance, dynamically generate a mask as needed, but the need for a transparent bitmap must specify a transparent color, wherever this is transparent, it is transparent.

The Code implemented with VC ++ MFC is as follows:
/*
This is a function used to draw a transparent bitmap.
CDC * the CDC pointer for which the pDC needs to draw bitmap
UINT IDImage bitmap resource ID
CRect & rect specifies the position of the bitmap in pDC
Transparent color of COLORREF rgbMask bitmap
*/
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, SRCCOPY );

ImageDC. SetBkColor (#000000 );
ImageDC. SetTextColor (# ffffff );
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. Left, rect. Top, rect. Width (), rect. Height (), & imagedc, 0, 0, srcpaint );

Maskdc. SelectObject (poldmaskdcbitmap );
Imagedc. SelectObject (poldimage );
}

Void cdemodlg: onpaint ()
{
Cpaintdc DC (this );

Cbitmap BMP back, * poldback ,;
BMP back. loadbitmap (idb_background );

CDC dcback;
Dcback. createcompatibledc (& DC );
Poldback = dcback. SelectObject (& BMP back );

DC. bitblt (100,100, & dcback, srccopy );
Drawtransparentbitmap (& DC, idb_bitmapa, crect (0, 0, 48), # c0c000 );

Dcback. SelectObject (poldback );
}

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.