none
TransparentBitmap RRS feed

  • 问题

  • The following piece of code how to remove the background color bitmap. I am a novice. Please reply to the correct code.

    TransparentBitmap(HDC hdc, HBITMAP hBitmap,short xStart,
    short yStart, short xadd,short yadd, COLORREF cTransparentColor)

    BITMAP m_bm;
    COLORREF cColor;

    //  创建临时 DC
    HDC hMem, hBack, hObject, hTemp, hSave;
    hBack  = CreateCompatibleDC(hdc);
    hObject = CreateCompatibleDC(hdc);
    hMem  = CreateCompatibleDC(hdc);
     hSave = CreateCompatibleDC(hdc);
     hTemp   = CreateCompatibleDC(hdc);
    //  选入位图
    SelectObject(hTemp, hBitmap);
    GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&m_bm);
    //显示位图宽高
    POINT ptSize;
    //  取得位图的宽度
    ptSize.x = m_bm.bmWidth;
    //  取得位图的该度
    ptSize.y = m_bm.bmHeight;
    //  转换为逻辑点值
    DPtoLP(hTemp, &ptSize, 1);

    //  创建临时位图
    HBITMAP bmBack, bmObject, bmMem, bmSave;
    //  单色位图
     
    bmBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
    bmObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
    //  与设备兼容位图
    bmMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
    bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);

    //  将创建的临时位图选入临时 DC 中
    HBITMAP   OldbmBack, OldbmObject, OldbmMem, OldbmSave;
    OldbmBack  = (HBITMAP)SelectObject(hBack, bmBack);
     OldbmObject = (HBITMAP)SelectObject(hObject, bmObject);
     OldbmMem    = (HBITMAP)SelectObject(hMem, bmMem);
     OldbmSave = (HBITMAP)SelectObject(hSave, bmSave);

    //  设置映射模式
    SetMapMode(hTemp, GetMapMode(hdc));
    //  先保留原始位图
    BitBlt(hSave, 0, 0, ptSize.x, ptSize.y, hTemp, 0, 0, SRCCOPY);
    //  将背景颜色设置为需透明的颜色
    cColor = SetBkColor(hTemp, RGB(255, 0, 255));
    //  创建目标屏蔽码
    BitBlt(hObject, 0, 0, ptSize.x, ptSize.y, hTemp, 0, 0, SRCCOPY);
    //  恢复源 DC 的原始背景色
    SetBkColor(hTemp, cColor);

    //  创建反转的目标屏蔽码
    BitBlt(hBack, 0, 0, ptSize.x, ptSize.y, hObject, 0, 0, NOTSRCCOPY);
    //  拷贝主 DC 的背景到目标 DC
    BitBlt(hMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart, SRCCOPY);
    //  屏蔽位图的显示区
    BitBlt(hMem, 0, 0, ptSize.x, ptSize.y, hObject, 0, 0, SRCAND);
    //  屏蔽位图中的透明色
    BitBlt(hTemp, 0, 0, ptSize.x, ptSize.y, hBack, 0, 0, SRCAND);
    //  将位图与目标 DC 的背景左异或操作
    BitBlt(hMem, 0, 0, ptSize.x, ptSize.y, hTemp, 0, 0, SRCPAINT);
    //  拷贝目标到屏幕上
    StretchBlt(hdc,  xStart,  yStart,  ptSize.x+xadd,  ptSize.y+yadd,  hMem,  0,  0,  ptSize.x, ptSize.y,SRCCOPY);
    //  恢复原始位图
    BitBlt(hTemp, 0, 0, ptSize.x, ptSize.y, hSave, 0, 0, SRCCOPY);

    //  删除临时内存位图 DeleteObject(SelectObject(hBack, OldbmBack)); DeleteObject(SelectObject(hObject, OldbmObject)); DeleteObject(SelectObject(hMem, OldbmMem)); DeleteObject(SelectObject(hSave, OldbmSave));

    //  删除临时内存 DC
    DeleteDC(hMem); DeleteDC(hBack); DeleteDC(hObject); DeleteDC(hSave); DeleteDC(hTemp);
    }

    2013年9月5日 1:54

答案

  • CBitmap DispBmp;
    CBitmap *OldBmp;
    CDC SourceDC;
    DispBmp.LoadBitmap(IDB_BITMAP1);
    BITMAP bmp;
    GetObject(DispBmp, sizeof(bmp), &bmp);
    SourceDC.CreateCompatibleDC(&dc);
    OldBmp = SourceDC.SelectObject(&DispBmp);
    CRect rect;
    GetClientRect(&rect);
    rect.DeflateRect(10, 10);
    ::TransparentBlt(dc.GetSafeHdc(), rect.left, rect.top, rect.Width(), rect.Height(), SourceDC.m_hDC, 0, 0, bmp.bmWidth, bmp.bmHeight, RGB(0,0,0));
    SourceDC.SelectObject(OldBmp); 
    ----------------------------------------
    Try this ~

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2013年9月5日 2:51
    版主

全部回复

  • Why usethis functionafterthe bitmapbackgroundiswhite.

    This is another pice of code.

    CMy1View::CMy1View()
    {
     // TODO: add construction code here
     xStart=30;
    yStart=30;
    xadd=0; yadd=0; m_Bitmap.LoadBitmap(IDB_BITMAP1);
    cTransparentColor=RGB(255,255,255);

    }

    2013年9月5日 2:24
  • Why use this function after the bitmap background is white.

    This is another pice of code.

    CMy1View::CMy1View()
    {
    // TODO: add construction code here
    xStart=30;
    yStart=30;
    xadd=0; yadd=0; m_Bitmap.LoadBitmap(IDB_BITMAP1);
    cTransparentColor=RGB(255,255,255);

    }

    2013年9月5日 2:24
  • CBitmap DispBmp;
    CBitmap *OldBmp;
    CDC SourceDC;
    DispBmp.LoadBitmap(IDB_BITMAP1);
    BITMAP bmp;
    GetObject(DispBmp, sizeof(bmp), &bmp);
    SourceDC.CreateCompatibleDC(&dc);
    OldBmp = SourceDC.SelectObject(&DispBmp);
    CRect rect;
    GetClientRect(&rect);
    rect.DeflateRect(10, 10);
    ::TransparentBlt(dc.GetSafeHdc(), rect.left, rect.top, rect.Width(), rect.Height(), SourceDC.m_hDC, 0, 0, bmp.bmWidth, bmp.bmHeight, RGB(0,0,0));
    SourceDC.SelectObject(OldBmp); 
    ----------------------------------------
    Try this ~

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2013年9月5日 2:51
    版主
  • I put your code into this line like this:

    void CMy1View::OnTimer(UINT nIDEvent)
    {
     // TODO: Add your message handler code here and/or call default
      CBitmap DispBmp;
     CBitmap *OldBmp;
     CDC SourceDC;
     DispBmp.LoadBitmap(IDB_BITMAP1);
     BITMAP bmp;
     GetObject(DispBmp, sizeof(bmp), &bmp);
     SourceDC.CreateCompatibleDC(&dc);
     OldBmp = SourceDC.SelectObject(&DispBmp);
     CRect rect;
     GetClientRect(&rect);
     rect.DeflateRect(10, 10);
     TransparentBlt(dc.GetSafeHdc(), rect.left, rect.top, rect.Width(), rect.Height(), SourceDC.m_hDC, 0, 0, bmp.bmWidth, bmp.bmHeight, RGB(0,0,0));
     SourceDC.SelectObject(OldBmp);
     CView::OnTimer(nIDEvent);
    }

    Then came these two issues:

    'dc' : undeclared identifier

    left of '.GetSafeHdc' must have class/struct/union type

    How to correct these two  errors .

    2013年9月5日 7:23
  • What is the identifier  dc.
    2013年9月5日 7:29