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. Transparent Bitmaps

Transparent Bitmaps

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicshelptutorialworkspace
5 Posts 3 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.
  • K Offline
    K Offline
    kylur
    wrote on last edited by
    #1

    I'm developing an application that allows the user to create a bitmap from portions of "n" other source bitmaps. For example, starting with three bitmaps (A, B, and C), the user could select the top third of A, the middle third of B and the bottom third of C and create destination bitmap D. What I'd like to do is create a window that shows one of bitmap A, B, C.... (and has a dialog bar allowing selection of which one). A, B, and C are fully formed, that is, all pixels are defined. Bitmap D is undefined, that is, all pixels are (by default) white. I'd like to display D as a translucent bitmap on top of the selected source bitmap. It the user left-click+moves the mouse over the pair of bitmaps the pixels on A are copied up into D. Once all desired pixels of A are copied upto D, the user would change A to B, D would now be displayed on top of B and the user could copied pixels from B upto D. And so on for all source bitmaps. The undefined parts of D (ie the white pixels) I'd like to show as a "gray film", and the cursor would then be an "eraser" ie remove the "film" and allows the underlying bitmap to shine through. In theory this is all fairly easy. BUT, I'm stymied on the simple part of how do I create and display the translucent bitmap D? Attempts so far are: 1. create aDC_ with aBitmap_ 2. create dDC_ with dBitmap_ 3. create memDC_ with memBitmap_ 4. memDC_->BitBlt(..., aDC_..., SRCCOPY) 5. memDC_->BitBlt(..., dDC_, ..., ?) <=== this is the translucent bitmap 6. pDC->BitBlt(..., memDC_..., SRCCOPY); The question is how do a setup the DC_'s to get the translucency to work? Any help is much appreciated! Regards, Kylur.

    C 1 Reply Last reply
    0
    • K kylur

      I'm developing an application that allows the user to create a bitmap from portions of "n" other source bitmaps. For example, starting with three bitmaps (A, B, and C), the user could select the top third of A, the middle third of B and the bottom third of C and create destination bitmap D. What I'd like to do is create a window that shows one of bitmap A, B, C.... (and has a dialog bar allowing selection of which one). A, B, and C are fully formed, that is, all pixels are defined. Bitmap D is undefined, that is, all pixels are (by default) white. I'd like to display D as a translucent bitmap on top of the selected source bitmap. It the user left-click+moves the mouse over the pair of bitmaps the pixels on A are copied up into D. Once all desired pixels of A are copied upto D, the user would change A to B, D would now be displayed on top of B and the user could copied pixels from B upto D. And so on for all source bitmaps. The undefined parts of D (ie the white pixels) I'd like to show as a "gray film", and the cursor would then be an "eraser" ie remove the "film" and allows the underlying bitmap to shine through. In theory this is all fairly easy. BUT, I'm stymied on the simple part of how do I create and display the translucent bitmap D? Attempts so far are: 1. create aDC_ with aBitmap_ 2. create dDC_ with dBitmap_ 3. create memDC_ with memBitmap_ 4. memDC_->BitBlt(..., aDC_..., SRCCOPY) 5. memDC_->BitBlt(..., dDC_, ..., ?) <=== this is the translucent bitmap 6. pDC->BitBlt(..., memDC_..., SRCCOPY); The question is how do a setup the DC_'s to get the translucency to work? Any help is much appreciated! Regards, Kylur.

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      here's the classic MSDN version:

      BOOL DrawTransparentBitmap(HDC hdc,
      HBITMAP hBitmap,
      __int32 xStart,
      __int32 yStart,
      COLORREF cTransparentColor)
      {

      BITMAP     bm;
      COLORREF   cColor;
      HBITMAP    bmAndBack, bmAndObject, bmAndMem, bmSave;			// lots of storage
      HBITMAP    bmBackOld, bmObjectOld, bmMemOld, bmSaveOld, bmTempOld;
      HDC        hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;		// lots of dc's
      POINT      ptSize;
      
      int iCaps = ::GetDeviceCaps(hdc, RASTERCAPS);
      if ((iCaps & RC\_BITBLT) != RC\_BITBLT)
      {
      	return FALSE;
      }
      
      if ((iCaps & RC\_BITMAP64) != RC\_BITMAP64)
      {
      	return FALSE;
      }
      
      hdcTemp = CreateCompatibleDC(hdc);
      
      if (hdcTemp==NULL) 
          {
      	return FALSE;
      }
      
      bmTempOld = (HBITMAP)SelectObject(hdcTemp, hBitmap);   // Select the bitmap
      
      if (GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm) != sizeof(BITMAP))
      

      {
      DeleteDC(hdcTemp);
      return FALSE;
      }

      ptSize.x = bm.bmWidth;            // Get width of bitmap
      ptSize.y = bm.bmHeight;           // Get height of bitmap
      DPtoLP(hdcTemp, &ptSize, 1);      // Convert from device
      // to logical points
      
      // Create some DCs to hold temporary data.
      hdcBack   = CreateCompatibleDC(hdc);
      hdcObject = CreateCompatibleDC(hdc);
      hdcMem    = CreateCompatibleDC(hdc);
      hdcSave   = CreateCompatibleDC(hdc);
      
      // Create a bitmap for each DC. DCs are required for a number of
      // GDI functions.
      
      // Monochrome DC
      bmAndBack   = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
      
      // Monochrome DC
      bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
      
      bmAndMem    = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
      bmSave      = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
      
      // Each DC must select a bitmap object to store pixel data.
      // (HBITMAP) Casts added by CDL, 3/26/96
      bmBackOld   = (HBITMAP)SelectObject(hdcBack, bmAndBack);
      bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject);
      bmMemOld    = (HBITMAP)SelectObject(hdcMem, bmAndMem);
      bmSaveOld   = (HBITMAP)SelectObject(hdcSave, bmSave);
      
      // Set proper mapping mode.
      SetMapMode(hdcTemp, GetMapMode(hdc));
      
      // Save the bitmap sent here, because it will be overwritten.
      BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
      
      // Set the background color of the source DC to the color.
      // contained in the parts of the bitmap that should be transparent
      cColor = SetBkColor(hdcTemp, cTransparentColor);
      
      // Create the object mask for the bitmap by perfo
      
      L 1 Reply Last reply
      0
      • C Chris Losinger

        here's the classic MSDN version:

        BOOL DrawTransparentBitmap(HDC hdc,
        HBITMAP hBitmap,
        __int32 xStart,
        __int32 yStart,
        COLORREF cTransparentColor)
        {

        BITMAP     bm;
        COLORREF   cColor;
        HBITMAP    bmAndBack, bmAndObject, bmAndMem, bmSave;			// lots of storage
        HBITMAP    bmBackOld, bmObjectOld, bmMemOld, bmSaveOld, bmTempOld;
        HDC        hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;		// lots of dc's
        POINT      ptSize;
        
        int iCaps = ::GetDeviceCaps(hdc, RASTERCAPS);
        if ((iCaps & RC\_BITBLT) != RC\_BITBLT)
        {
        	return FALSE;
        }
        
        if ((iCaps & RC\_BITMAP64) != RC\_BITMAP64)
        {
        	return FALSE;
        }
        
        hdcTemp = CreateCompatibleDC(hdc);
        
        if (hdcTemp==NULL) 
            {
        	return FALSE;
        }
        
        bmTempOld = (HBITMAP)SelectObject(hdcTemp, hBitmap);   // Select the bitmap
        
        if (GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm) != sizeof(BITMAP))
        

        {
        DeleteDC(hdcTemp);
        return FALSE;
        }

        ptSize.x = bm.bmWidth;            // Get width of bitmap
        ptSize.y = bm.bmHeight;           // Get height of bitmap
        DPtoLP(hdcTemp, &ptSize, 1);      // Convert from device
        // to logical points
        
        // Create some DCs to hold temporary data.
        hdcBack   = CreateCompatibleDC(hdc);
        hdcObject = CreateCompatibleDC(hdc);
        hdcMem    = CreateCompatibleDC(hdc);
        hdcSave   = CreateCompatibleDC(hdc);
        
        // Create a bitmap for each DC. DCs are required for a number of
        // GDI functions.
        
        // Monochrome DC
        bmAndBack   = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
        
        // Monochrome DC
        bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
        
        bmAndMem    = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
        bmSave      = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
        
        // Each DC must select a bitmap object to store pixel data.
        // (HBITMAP) Casts added by CDL, 3/26/96
        bmBackOld   = (HBITMAP)SelectObject(hdcBack, bmAndBack);
        bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject);
        bmMemOld    = (HBITMAP)SelectObject(hdcMem, bmAndMem);
        bmSaveOld   = (HBITMAP)SelectObject(hdcSave, bmSave);
        
        // Set proper mapping mode.
        SetMapMode(hdcTemp, GetMapMode(hdc));
        
        // Save the bitmap sent here, because it will be overwritten.
        BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
        
        // Set the background color of the source DC to the color.
        // contained in the parts of the bitmap that should be transparent
        cColor = SetBkColor(hdcTemp, cTransparentColor);
        
        // Create the object mask for the bitmap by perfo
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It's obsolete. Use Shell : 4 lines of code (!)

        K C 2 Replies Last reply
        0
        • L Lost User

          It's obsolete. Use Shell : 4 lines of code (!)

          K Offline
          K Offline
          kylur
          wrote on last edited by
          #4

          And the four line would be?..... (Or at least where should I look to find them myself?)

          1 Reply Last reply
          0
          • L Lost User

            It's obsolete. Use Shell : 4 lines of code (!)

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            GDI is not obsolete.

            image processing toolkits | batch image processing

            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