Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. transp. blit over background image

transp. blit over background image

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TimWallace
    wrote on last edited by
    #1

    I have a need to blit a .bmp over a backgound image, but not obscure areas under the foreground image where pixels are deemed to be transparent. I don't want the user to have to supply a mask image. I've read the CP articles on creating masks on the fly, but I just can't seem to wrap my mind around how to blend the two images. If someone can give me some guidance/pointers, I will be greatly appreciative. To make things even more fun, I'm coding in pure Win 32 - no MFC or ATL! Thanks in advance. things could always be worse...

    E T 2 Replies Last reply
    0
    • T TimWallace

      I have a need to blit a .bmp over a backgound image, but not obscure areas under the foreground image where pixels are deemed to be transparent. I don't want the user to have to supply a mask image. I've read the CP articles on creating masks on the fly, but I just can't seem to wrap my mind around how to blend the two images. If someone can give me some guidance/pointers, I will be greatly appreciative. To make things even more fun, I'm coding in pure Win 32 - no MFC or ATL! Thanks in advance. things could always be worse...

      E Offline
      E Offline
      Ernest Laurentin
      wrote on last edited by
      #2

      This is the most known method unless you want to use TransparentBlt function. There is not much to say, other than the bitmap 'hBitmap' must not be already selected in another dc (only one dc can select a bitmap at a time!)

      void DrawTransparent(HDC hDC, int x, int y, int nWidth, int nHeight, HBITMAP hBitmap, COLORREF crColour)
      {
      _ASSERTE(hDC != NULL);
      _ASSERTE(hBitmap != NULL);

      COLORREF crOldBack = SetBkColor(hDC, RGB(255,255,255));
      COLORREF crOldText = SetTextColor(hDC, RGB(0,0,0));
      
      HDC dcImage, dcMask, dcMem;
      // Create two memory dcs for the image and the mask
      dcImage = CreateCompatibleDC(hDC);
      dcMask  = CreateCompatibleDC(hDC);
      dcMem   = CreateCompatibleDC(hDC);
      
      HBITMAP hOldMemBitmap = (HBITMAP) SelectObject(dcMem, hBitmap);
      
      // Create bitmap copy
      HBITMAP hBitmapImage = CreateCompatibleBitmap(hDC, nWidth, nHeight);
      
      // Select the image into the appropriate dc
      HBITMAP hOldBitmapImage = (HBITMAP) SelectObject(dcImage, hBitmapImage);
      
      HBITMAP bitmapMask = CreateBitmap(nWidth, nHeight, 1, 1, NULL);
      
      // Select the mask bitmap into the appropriate dc
      HBITMAP hOldBitmapMask = (HBITMAP) SelectObject(dcMask, bitmapMask);
      
      // Build mask based on transparent colour
      BitBlt(dcImage, 0, 0, nWidth, nHeight, dcMem, 0, 0, SRCCOPY);
      
      SetBkColor(dcImage, crColour);
      BitBlt(dcMask, 0, 0, nWidth, nHeight, dcImage, 0, 0, SRCCOPY);
      
      // Do the work - True Mask method - cool if not actual display
      BitBlt(hDC, x, y, nWidth, nHeight, dcImage, 0, 0, SRCINVERT);
      BitBlt(hDC, x, y, nWidth, nHeight, dcMask,  0, 0, SRCAND);
      BitBlt(hDC, x, y, nWidth, nHeight, dcImage, 0, 0, SRCINVERT);
      
      // Restore settings
      SelectObject(dcImage, hOldBitmapImage);
      SelectObject(dcMask,  hOldBitmapMask);
      SelectObject(dcMem, hOldMemBitmap);
      
      DeleteObject(hBitmapImage);
      DeleteObject(bitmapMask);
      
      DeleteDC( dcMem );
      DeleteDC( dcMask );
      DeleteDC( dcImage );
      
      SetBkColor(hDC, crOldBack);
      SetTextColor(hDC, crOldText);
      

      }

      ÿVOTD: 8 "Love never fails. But where there are prophecies, they will cease; where there are tongues, they will be stilled; where there is knowledge, it will pass away." - 1 Cor 13:8 (NIV)

      1 Reply Last reply
      0
      • T TimWallace

        I have a need to blit a .bmp over a backgound image, but not obscure areas under the foreground image where pixels are deemed to be transparent. I don't want the user to have to supply a mask image. I've read the CP articles on creating masks on the fly, but I just can't seem to wrap my mind around how to blend the two images. If someone can give me some guidance/pointers, I will be greatly appreciative. To make things even more fun, I'm coding in pure Win 32 - no MFC or ATL! Thanks in advance. things could always be worse...

        T Offline
        T Offline
        TimWallace
        wrote on last edited by
        #3

        I figured it out. I case anyone else is interested, I did it as follows: // sets up the mask BitBlt(hdcDst,31,234,43,36,hdcSrc,31,234,SRCCOPY); clrOldText = SetTextColor(hdcSrc,RGB(255,255,255)); SetBkColor(hdcSrc,RGB(0,0,0)); BitBlt(hdcSrc,31,234,43,36,hdcTmp,31,234,SRCAND); BitBlt(hDC,31,234,43,36,hdcTmp,31,234,SRCAND); BitBlt(hDC,31,234,43,36,hdcSrc,31,234,SRCPAINT); If anyone sees any inherent flaws herein, please point them out. My colors looked right and nothing was obscured on the background, so I called it 'fixed'.:) things could always be worse...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups