투명한 비트맵 처리를 위한 C 코드
Posted 2008/06/19 17:39|
|
|
댓글 하나가 운영자에겐 커다란 힘이 됩니다!
투명한 비트맵 처리를 위한 C 코드
01: static void DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap,
02:
03: LONG xStart, LONG yStart, COLORREF cTransparentColor)
04: {
05: BITMAP bm;
06: COLORREF cColor;
07: HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
08: HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
09: HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
10: POINT ptSize;
11:
12: hdcTemp = CreateCompatibleDC(hdc);
13: SelectObject(hdcTemp, hBitmap); // Select the bitmap 14:
15: GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
16: ptSize.x = bm.bmWidth; // Get width of bitmap 17: ptSize.y = bm.bmHeight; // Get height of bitmap 18: DPtoLP(hdcTemp, &ptSize, 1); // Convert from device 19: // to logical points 20:
21: // Create some DCs to hold temporary data. 22: hdcBack = CreateCompatibleDC(hdc);
23: hdcObject = CreateCompatibleDC(hdc);
24: hdcMem = CreateCompatibleDC(hdc);
25: hdcSave = CreateCompatibleDC(hdc);
26:
27: // Create a bitmap for each DC. DCs are required for a number of 28: // GDI functions. 29:
30: // Monochrome DC 31: bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
32:
33: // Monochrome DC 34: bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
35:
36: bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
37: bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
38:
39: // Each DC must select a bitmap object to store pixel data. 40: bmBackOld = (HBITMAP)SelectObject(hdcBack, bmAndBack);
41: bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject);
42: bmMemOld = (HBITMAP)SelectObject(hdcMem, bmAndMem);
43: bmSaveOld = (HBITMAP)SelectObject(hdcSave, bmSave);
44:
45: // Set proper mapping mode. 46: SetMapMode(hdcTemp, GetMapMode(hdc));
47:
48: // Save the bitmap sent here, because it will be overwritten. 49: BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
50:
51: // Set the background color of the source DC to the color. 52: // contained in the parts of the bitmap that should be transparent 53: cColor = SetBkColor(hdcTemp, cTransparentColor);
54:
55: // Create the object mask for the bitmap by performing a BitBlt 56: // from the source bitmap to a monochrome bitmap. 57: BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
58: SRCCOPY);
59:
60: // Set the background color of the source DC back to the original 61: // color. 62: SetBkColor(hdcTemp, cColor);
63:
64: // Create the inverse of the object mask. 65: BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
66: NOTSRCCOPY);
67:
68: // Copy the background of the main DC to the destination. 69: BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
70: SRCCOPY);
71:
72: // Mask out the places where the bitmap will be placed. 73: BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
74:
75: // Mask out the transparent colored pixels on the bitmap. 76: BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
77:
78: // XOR the bitmap with the background on the destination DC. 79: BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
80:
81: // Copy the destination to the screen. 82: BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
83: SRCCOPY);
84:
85: // Place the original bitmap back into the bitmap sent here. 86: BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
87:
88: // Delete the memory bitmaps. 89: DeleteObject(SelectObject(hdcBack, bmBackOld));
90: DeleteObject(SelectObject(hdcObject, bmObjectOld));
91: DeleteObject(SelectObject(hdcMem, bmMemOld));
92: DeleteObject(SelectObject(hdcSave, bmSaveOld));
93:
94: // Delete the memory DCs. 95: DeleteDC(hdcMem);
96: DeleteDC(hdcBack);
97: DeleteDC(hdcObject);
98: DeleteDC(hdcSave);
99: DeleteDC(hdcTemp);
100: }
위의 정보가 도움이 되셨나요? 그렇다면 댓글 하나만 남겨주세요.
댓글 하나가 운영자에겐 커다란 힘이 됩니다!
- Filed under : 프로그래밍/C & Cpp
- Tag : bitmap, CODE, transparent, 비트맵, 처리, 코드, 투명
- Comment Trackback

