去掉图片背景色

 BITMAP     m_bm;
   COLORREF   cColor;
   // ´´½¨ÁÙʱDC
   HDC     hMem, hBack, hObject, hTemp, hSave;
   hBack   = CreateCompatibleDC(hdc);
   hObject = CreateCompatibleDC(hdc);
   hMem    = CreateCompatibleDC(hdc);
   hSave   = CreateCompatibleDC(hdc);
   hTemp   = CreateCompatibleDC(hdc);
   // Ñ¡Èëλͼ
   SelectObject(hTemp, hBitmap);
   GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&m_bm);
   //ÏÔʾλͼ¿í¸ß
   POINT      ptSize;
   // È¡µÃλͼµÄ¿í¶È
   ptSize.x = m_bm.bmWidth;
    // È¡µÃλͼµÄ¸Ã¶È
   ptSize.y = m_bm.bmHeight;
   // ת»»ÎªÂß¼­µãÖµ
   DPtoLP(hTemp, &ptSize, 1);
   // ´´½¨ÁÙʱλͼ
   HBITMAP    bmBack, bmObject, bmMem, bmSave;
   // µ¥É«Î»Í¼
   bmBack   = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
   bmObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
   // ÓëÉ豸¼æÈÝλͼ
   bmMem    = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
   bmSave      = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
   // ½«´´½¨µÄÁÙʱλͼѡÈëÁÙʱDCÖÐ
   HBITMAP    OldbmBack, OldbmObject, OldbmMem, OldbmSave;
   OldbmBack   = (HBITMAP)SelectObject(hBack, bmBack);
   OldbmObject = (HBITMAP)SelectObject(hObject, bmObject);
   OldbmMem    = (HBITMAP)SelectObject(hMem, bmMem);
   OldbmSave   = (HBITMAP)SelectObject(hSave, bmSave);      // ÉèÖÃÓ³Éäģʽ
   SetMapMode(hTemp, GetMapMode(hdc));             // Ïȱ£Áôԭʼλͼ
   BitBlt(hSave, 0, 0, ptSize.x, ptSize.y, hTemp, 0, 0, SRCCOPY);     // ½«±³¾°ÑÕÉ«ÉèÖÃΪÐè͸Ã÷µÄÑÕÉ«
   cColor = SetBkColor(hTemp, cTransparentColor);    // ´´½¨Ä¿±êÆÁ±ÎÂë
   BitBlt(hObject, 0, 0, ptSize.x, ptSize.y, hTemp, 0, 0, SRCCOPY);      // »Ö¸´Ô´DCµÄԭʼ±³¾°É«
   SetBkColor(hTemp, cColor);   // ´´½¨·´×ªµÄÄ¿±êÆÁ±ÎÂë
   BitBlt(hBack, 0, 0, ptSize.x, ptSize.y, hObject, 0, 0, NOTSRCCOPY);
   // ¿½±´Ö÷DCµÄ±³¾°µ½Ä¿±êDC
   BitBlt(hMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart, SRCCOPY);
   // ÆÁ±ÎλͼµÄÏÔʾÇø
   BitBlt(hMem, 0, 0, ptSize.x, ptSize.y, hObject, 0, 0, SRCAND);
   // ÆÁ±ÎλͼÖеÄ͸Ã÷É«
   BitBlt(hTemp, 0, 0, ptSize.x, ptSize.y, hBack, 0, 0, SRCAND);
   // ½«Î»Í¼ÓëÄ¿±êDCµÄ±³¾°×óÒì»ò²Ù×÷
   BitBlt(hMem, 0, 0, ptSize.x, ptSize.y, hTemp, 0, 0, SRCPAINT);
   // ¿½±´Ä¿±êµ½ÆÁÄ»ÉÏ
   StretchBlt(hdc, xStart, yStart, ptSize.x+xadd, ptSize.y+yadd, hMem, 0, 0, ptSize.x, ptSize.y,SRCCOPY);
   // »Ö¸´Ô­Ê¼Î»Í¼
   BitBlt(hTemp, 0, 0, ptSize.x, ptSize.y, hSave, 0, 0, SRCCOPY);
   // ɾ³ýÁÙʱÄÚ´æλͼ
   DeleteObject(SelectObject(hBack, OldbmBack));
   DeleteObject(SelectObject(hObject, OldbmObject));
   DeleteObject(SelectObject(hMem, OldbmMem));
   DeleteObject(SelectObject(hSave, OldbmSave));
   // ɾ³ýÁÙʱÄÚ´æDC
   DeleteDC(hMem);
   DeleteDC(hBack);
   DeleteDC(hObject);
   DeleteDC(hSave);
   DeleteDC(hTemp);

你可能感兴趣的:(null)