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 display a bitmap with an alpha chanel?

how can I display a bitmap with an alpha chanel?

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphics
9 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.
  • D Offline
    D Offline
    danginkgo
    wrote on last edited by
    #1

    I want to display a bitmap that has an alpha chanel for the background. How can I make the alpha chanel transparent? I tried using AlphaBlend() but it doesn't work. Any suggestions?

    danginkgo

    R M 2 Replies Last reply
    0
    • D danginkgo

      I want to display a bitmap that has an alpha chanel for the background. How can I make the alpha chanel transparent? I tried using AlphaBlend() but it doesn't work. Any suggestions?

      danginkgo

      R Offline
      R Offline
      rp_suman
      wrote on last edited by
      #2

      Hi See if this helps you: bitmap - alpha channel[^]

      -- "Programming is an art that fights back!"

      D 1 Reply Last reply
      0
      • R rp_suman

        Hi See if this helps you: bitmap - alpha channel[^]

        -- "Programming is an art that fights back!"

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

        I can't figure it out... What shall I use to dispaly the bmp?

        danginkgo

        R 1 Reply Last reply
        0
        • D danginkgo

          I can't figure it out... What shall I use to dispaly the bmp?

          danginkgo

          R Offline
          R Offline
          rp_suman
          wrote on last edited by
          #4

          Hi, Here is an article explaining alpha blending: Alpha Blending using GDI+[^] Regards, Suman

          -- "Programming is an art that fights back!"

          D 1 Reply Last reply
          0
          • D danginkgo

            I want to display a bitmap that has an alpha chanel for the background. How can I make the alpha chanel transparent? I tried using AlphaBlend() but it doesn't work. Any suggestions?

            danginkgo

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

            danginkgo wrote:

            I tried using AlphaBlend() but it doesn't work.

            How did you do this? What values did you use in the BLENDFUNCTION struct? Is the bitmap 32bpp? What does "doesn't work" mean.

            D 1 Reply Last reply
            0
            • M Mark Salsbery

              danginkgo wrote:

              I tried using AlphaBlend() but it doesn't work.

              How did you do this? What values did you use in the BLENDFUNCTION struct? Is the bitmap 32bpp? What does "doesn't work" mean.

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

              This is my BLENDFUNCTION struct: BLENDFUNCTION blendF; blendF.BlendOp = AC_SRC_OVER; blendF.BlendFlags = 0; blendF.SourceConstantAlpha = 255; blendF.AlphaFormat = AC_SRC_ALPHA; The bitmap is 32 bpp with an alpha chanel. When I display the image using AlphaBlend() function the image looks the same as when I displayed it using BitBlt(). I want the background (wich has an alpha chanel) to be transparent. Is it posible using this function?

              danginkgo

              M 1 Reply Last reply
              0
              • R rp_suman

                Hi, Here is an article explaining alpha blending: Alpha Blending using GDI+[^] Regards, Suman

                -- "Programming is an art that fights back!"

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

                I am using GDI. Is alpha blending posible only with GDI+ ? I want to blend only the background. Thank You for the answers.

                danginkgo

                R 1 Reply Last reply
                0
                • D danginkgo

                  I am using GDI. Is alpha blending posible only with GDI+ ? I want to blend only the background. Thank You for the answers.

                  danginkgo

                  R Offline
                  R Offline
                  rp_suman
                  wrote on last edited by
                  #8

                  HI I dont know the differences between GDI & GDI+, but you can do it using GDI. Refer here: Windows GDI - Alpha Blending[^] A sample here: Alpha Blending a Bitmap[^]

                  -- "Programming is an art that fights back!"

                  1 Reply Last reply
                  0
                  • D danginkgo

                    This is my BLENDFUNCTION struct: BLENDFUNCTION blendF; blendF.BlendOp = AC_SRC_OVER; blendF.BlendFlags = 0; blendF.SourceConstantAlpha = 255; blendF.AlphaFormat = AC_SRC_ALPHA; The bitmap is 32 bpp with an alpha chanel. When I display the image using AlphaBlend() function the image looks the same as when I displayed it using BitBlt(). I want the background (wich has an alpha chanel) to be transparent. Is it posible using this function?

                    danginkgo

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

                    danginkgo wrote:

                    When I display the image using AlphaBlend() function the image looks the same as when I displayed it using BitBlt().

                    Then all the alpha channel values must be 255.

                    danginkgo wrote:

                    Is it posible using this function?

                    Yes. Try something like this, creating and rendering your own 32bpp ARGB bitmap:

                    LONG lImageWidth = 640;
                    LONG lImageHeight = 480;
                    WORD wBitsPerPixel = 32;

                    LONG lStride = lImageWidth * 4;

                    BYTE* pBitmapBits;

                    BITMAPINFO bmi;
                    memset(&bmi, 0, sizeof(BITMAPINFO));
                    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
                    bmi.bmiHeader.biWidth = lImageWidth;
                    bmi.bmiHeader.biHeight = lImageHeight;
                    bmi.bmiHeader.biPlanes = 1;
                    bmi.bmiHeader.biBitCount = wBitsPerPixel;
                    bmi.bmiHeader.biCompression = BI_RGB;
                    bmi.bmiHeader.biSizeImage = lStride * lImageHeight;
                    //bmi.bmiHeader.biXPelsPerMeter = 0;
                    //bmi.bmiHeader.biYPelsPerMeter = 0;
                    //bmi.bmiHeader.biClrUsed = 0;
                    //bmi.bmiHeader.biClrImportant = 0;

                    HDC hdcMem = ::CreateCompatibleDC(0);

                    HBITMAP hBitmap = ::CreateDIBSection(hdcMem, &bmi, DIB_RGB_COLORS, (void**)&pBitmapBits, NULL, 0);

                    if (hBitmap)
                    {
                    memset(pBitmapBits, 0, bmi.bmiHeader.biSizeImage);
                    RGBQUAD *pCurPixel = (RGBQUAD *)pBitmapBits;
                    int PixelCount = lImageWidth * lImageHeight;
                    while (PixelCount > 0)
                    {
                    (*pCurPixel).rgbRed = 0x00;
                    (*pCurPixel).rgbGreen = 0x00;
                    (*pCurPixel).rgbBlue = 0xA0;
                    (*pCurPixel).rgbReserved = 0x80;
                    pCurPixel++;
                    PixelCount--;
                    }

                    HGDIOBJ hOldBitmap = ::SelectObject(hdcMem, hBitmap);
                    
                    HDC hClientDC = ::GetDC(\*this);
                    
                    HPEN hPen = ::CreatePen(PS\_SOLID, 10, RGB(255,0,128));
                    HGDIOBJ hOldPen = ::SelectObject(hClientDC, hPen);
                    ::MoveToEx(hClientDC, 0, 0, NULL);
                    ::LineTo(hClientDC, 650, 490);
                    ::SelectObject(hClientDC, hOldPen);
                    ::DeleteObject(hPen);
                    
                    BLENDFUNCTION bf;
                    bf.BlendOp = AC\_SRC\_OVER;
                    bf.BlendFlags = 0;
                    bf.SourceConstantAlpha = 0xFF;
                    bf.AlphaFormat = AC\_SRC\_ALPHA;
                    
                    ::AlphaBlend(hClientDC, 0, 0, lImageWidth, lImageHeight,
                    					hdcMem, 0, 0, lImageWidth, lImageHeight, bf);
                    
                    ::ReleaseDC(\*this, hClientDC);
                    ::SelectObject(hdcMem, hOldBitmap);
                    ::DeleteObject(hBitmap);
                    

                    }

                    ::DeleteDC(hdcMem);

                    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