CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 1999
    Posts
    85

    Adding Bitmap resource to Bitmap file transparency issue

    Hi,

    I have a transparency issue when adding a mouse pointer (Bitmap resource) onto an existing Bitmap image.
    How do you set the transparent color of the bitmap image to apply?

    Currently the bitmap is all white except a black outline of the mouse pointer, when I use code below with SRCAND, the middle is NOT solid it is also transparent. If I fill the mouse pointer middle Grey then it’s ok, If I fill the outside green then how do I make this part transparent so I can have a white mouse pointer solid?



    void CSnapScreen::ApplyMousePointer(CBitmap &m_BmpTarget, int m_width, int m_height, int mouseX, int mouseY)
    {
    CBitmap m_Bmp1;
    if(m_Bmp1.LoadBitmap(IDB_IMG1)==0) // IDB_IMG1 is mouse cursor bitmap
    {
    AfxMessageBox("Load mouse pointer error");
    }


    int nTargetWidth = m_width;
    int nTargetHeight = m_height;
    int nCoordX = mouseX+4;
    int nCoordY = mouseY+4;

    CBitmap* pBmpSource = &m_Bmp1;
    CBitmap* pOldTargetBmp = NULL;
    CBitmap* pOldSourceBmp = NULL;
    CDC targetDC;
    CDC sourceDC;
    CDC* pDC = this->GetDC();

    // create device contexts for moving the bitmaps around
    targetDC.CreateCompatibleDC(pDC);
    sourceDC.CreateCompatibleDC(pDC);

    pOldTargetBmp = targetDC.SelectObject(&m_BmpTarget);
    pOldSourceBmp = sourceDC.SelectObject(pBmpSource); // cursor


    // bitblt the source DC onto the target DC
    if(targetDC.BitBlt(nCoordX, nCoordY, nTargetWidth, nTargetHeight, &sourceDC, 0, 0, SRCAND )==0) //SRCAND
    {
    AfxMessageBox("Error adding mouse pointer to bitmap!");
    }

    // re-select the original (default) bitmap so we don't leak resources
    sourceDC.SelectObject(pOldSourceBmp);
    // re-select the original (default) bitmap so we don't leak resources
    targetDC.SelectObject(pOldTargetBmp);


    sourceDC.DeleteDC();
    targetDC.DeleteDC();
    ReleaseDC(pDC);


    }

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Adding Bitmap resource to Bitmap file transparency issue

    In Windows, transparency of bitmaps is managed with the alpha channel.

    The alpha channel did not exist when I started to learn Windows programming, so I relied on something else which is a method where you have two bitmaps instead of just one. A first bitmap is a mask, and a second bitmap is a colored picture. See http://en.wikipedia.org/wiki/Image_mask#Image_masks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured