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. How can I use AlphaBlend API to display a transparent background bitmap?

How can I use AlphaBlend API to display a transparent background bitmap?

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsjson
14 Posts 5 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.
  • P Paresh Chitte

    Hi, Will TransparentBlt[^] helpful ? Regards, Paresh.

    D Offline
    D Offline
    danginkgo
    wrote on last edited by
    #4

    No, because the background is white, but there are some other white pixels within the bitmap. I don't want that all the white pixels to be transparent. The alpha channel is defined only for the background pixels.

    danginkgo

    1 Reply Last reply
    0
    • S SandipG

      danginkgo wrote:

      blendF.SourceConstantAlpha = 255;

      I think problem is value 255 which is opaque 0 value is complete transparent Try to specify some other value in range 0-255. I hope it helps.

      Regards, Sandip.

      D Offline
      D Offline
      danginkgo
      wrote on last edited by
      #5

      I tried other values but it did't display the way I need. I want only the background to be complete transparent. The rest of the image must be opaque.

      danginkgo

      S 1 Reply Last reply
      0
      • D danginkgo

        I tried other values but it did't display the way I need. I want only the background to be complete transparent. The rest of the image must be opaque.

        danginkgo

        S Offline
        S Offline
        SandipG
        wrote on last edited by
        #6

        You are not doing anything to background so how it will be transparent. This function will only affect the way source bitmap is combined with destination (background). and if you don't want background why are you drawing it??

        Regards, Sandip.

        D 1 Reply Last reply
        0
        • S SandipG

          You are not doing anything to background so how it will be transparent. This function will only affect the way source bitmap is combined with destination (background). and if you don't want background why are you drawing it??

          Regards, Sandip.

          D Offline
          D Offline
          danginkgo
          wrote on last edited by
          #7

          Ok, How can I draw the bitmap without the background?

          danginkgo

          S 1 Reply Last reply
          0
          • D danginkgo

            Ok, How can I draw the bitmap without the background?

            danginkgo

            S Offline
            S Offline
            SandipG
            wrote on last edited by
            #8

            danginkgo wrote:

            How can I draw the bitmap without the background?

            What do you mean? From your post understood following 1. There is a background 2. There is a bitmap which you need to draw on that background isn't it?? Can you explain what are you doing exactly??

            Regards, Sandip.

            D 1 Reply Last reply
            0
            • S SandipG

              danginkgo wrote:

              How can I draw the bitmap without the background?

              What do you mean? From your post understood following 1. There is a background 2. There is a bitmap which you need to draw on that background isn't it?? Can you explain what are you doing exactly??

              Regards, Sandip.

              D Offline
              D Offline
              danginkgo
              wrote on last edited by
              #9

              OK, For example, I want to display the bitmap on a red background. The bitmap image has also a background ( a black pen on a gray background ). I want the bitmap background to be transparent, so the image (black pen)will apear on red background.

              danginkgo

              S 1 Reply Last reply
              0
              • D danginkgo

                I have a bitmap that has an alpha channel for the background. I tried using AlphaBlend, but the background was't displayed transparent. The background was white. This is the code that I'm using: BLENDFUNCTION blendF; blendF.BlendOp = AC_SRC_OVER; blendF.BlendFlags = 0; blendF.SourceConstantAlpha = 255; blendF.AlphaFormat = AC_SRC_ALPHA; POINT p; p.x=p.y=23; DPtoLP(hdcMem, &p, 1); AlphaBlend(lpdis->hDC, x, y, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, p.x,p.y, blendF); Am I doing something wrong?

                danginkgo

                I Offline
                I Offline
                Iain Clarke Warrior Programmer
                wrote on last edited by
                #10

                Changing direction for the solution, I wrote some software a while ago that put together irregular chunks of bitmaps (a jigsaw game).

                	pDC->SetTextColor (RGB(255,255,255));
                	pDC->SetBkColor (RGB(0,0,0));
                	pDC->BitBlt (pt.x, pt.y, m\_rcOriginal.Width () + (2\*m\_nBorderSize), m\_rcOriginal.Height () + (2\*m\_nBorderSize), &m\_dcMaskBW, 0,0, SRCAND);
                	pDC->BitBlt (pt.x, pt.y, m\_rcOriginal.Width () + (2\*m\_nBorderSize), m\_rcOriginal.Height () + (2\*m\_nBorderSize), &m\_dcTile, 0,0, SRCPAINT);
                

                The SetText/BkColor are because the source DC was a 1bit colour depth DC, and acted as a mask. The background was white, and the middle area was black. So it made a black area on the destination. The second BitBlt ORed in a DC which had the interesting bits of the picture. The area I wanted to be transparent was black, not grey. This would mean adapting your code to two DCs, but that should not be too hard. It also would not work if you wanted partial transparency, but it does not appear that you do. Lastly, you could get flicker if you were unlucky - so my whole drawing code is wrapped using Keith Rule's excellent CMemDC class. Flicker Free Drawing In MFC[^] Iain.

                Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                D 1 Reply Last reply
                0
                • D danginkgo

                  OK, For example, I want to display the bitmap on a red background. The bitmap image has also a background ( a black pen on a gray background ). I want the bitmap background to be transparent, so the image (black pen)will apear on red background.

                  danginkgo

                  S Offline
                  S Offline
                  SandipG
                  wrote on last edited by
                  #11

                  Then you have to go for option given by Paresh i.e. TrasparentBlt.

                  Regards, Sandip.

                  D 1 Reply Last reply
                  0
                  • S SandipG

                    Then you have to go for option given by Paresh i.e. TrasparentBlt.

                    Regards, Sandip.

                    D Offline
                    D Offline
                    danginkgo
                    wrote on last edited by
                    #12

                    OK, thanks :)

                    danginkgo

                    1 Reply Last reply
                    0
                    • I Iain Clarke Warrior Programmer

                      Changing direction for the solution, I wrote some software a while ago that put together irregular chunks of bitmaps (a jigsaw game).

                      	pDC->SetTextColor (RGB(255,255,255));
                      	pDC->SetBkColor (RGB(0,0,0));
                      	pDC->BitBlt (pt.x, pt.y, m\_rcOriginal.Width () + (2\*m\_nBorderSize), m\_rcOriginal.Height () + (2\*m\_nBorderSize), &m\_dcMaskBW, 0,0, SRCAND);
                      	pDC->BitBlt (pt.x, pt.y, m\_rcOriginal.Width () + (2\*m\_nBorderSize), m\_rcOriginal.Height () + (2\*m\_nBorderSize), &m\_dcTile, 0,0, SRCPAINT);
                      

                      The SetText/BkColor are because the source DC was a 1bit colour depth DC, and acted as a mask. The background was white, and the middle area was black. So it made a black area on the destination. The second BitBlt ORed in a DC which had the interesting bits of the picture. The area I wanted to be transparent was black, not grey. This would mean adapting your code to two DCs, but that should not be too hard. It also would not work if you wanted partial transparency, but it does not appear that you do. Lastly, you could get flicker if you were unlucky - so my whole drawing code is wrapped using Keith Rule's excellent CMemDC class. Flicker Free Drawing In MFC[^] Iain.

                      Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                      D Offline
                      D Offline
                      danginkgo
                      wrote on last edited by
                      #13

                      OK, thank you :)

                      danginkgo

                      1 Reply Last reply
                      0
                      • D danginkgo

                        I have a bitmap that has an alpha channel for the background. I tried using AlphaBlend, but the background was't displayed transparent. The background was white. This is the code that I'm using: BLENDFUNCTION blendF; blendF.BlendOp = AC_SRC_OVER; blendF.BlendFlags = 0; blendF.SourceConstantAlpha = 255; blendF.AlphaFormat = AC_SRC_ALPHA; POINT p; p.x=p.y=23; DPtoLP(hdcMem, &p, 1); AlphaBlend(lpdis->hDC, x, y, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, p.x,p.y, blendF); Am I doing something wrong?

                        danginkgo

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #14

                        Yesterday, I gave you code that demonstrates AlphaBlend() does indeed work. Did you verify that your bitmap actually has alpha channel values set to something other than 255?

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        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