一种比较简单但比较慢的方法是逐个画点, CBitmap   bmp;
 CDC       dc;
 CPaintDC  pdc( this ); bmp.LoadBitmap( IDB_BACK );
 dc.CreateCompatibleDC( &pdc );
 dc.SelectObject( &bmp ); int       x, y;
 for( x = 0; x < BMP_WIDTH; x++ )
 {
     for( y = 0; y < BMP_HIGEHT; y++ )
     {
          COLORREF  pix;
          pix = dc.GetPixel();
          if( pix != RGB( 透明色 ) )
          {
               pdc.SetPixel( x, y, pix );
           }
      }
  }  当然你应该获得图像的尺寸.  还有一种方式摘抄如下:  绘制“透明”位图是指绘制某一位图中除指定颜色外的其余部分,我们称这种颜色为“透明色”。通过将位图的背景色指定为“透明色”,在绘制时,不绘制这部分背景,而仅绘制图像,这样就可以将位图中图像透明地绘制到窗口上。   绘制“透明”位图的关键是创建一个“掩码”位图(mask bitmap),“掩码”位图是一个单色位图,它是位图中图像的一个单色剪影。   在Windows编程中,绘图都要用到设备描述表,我们需创建两个内存设备描述表:位图设备描述表(image DC)和“掩码”位图设备描述表(mask DC)。位图设备描述表用来装入位图,而“掩码”位图设备描述表用来装入“掩码”位图。在“掩码”位图设备描述表中制作“掩码”位图的方式是:先创建一个单色的Bitmap,装入mask DC,然后,以“SRCCOPY”的方式将装有位图的位图设备描述表绘制(BitBlt)到mask DC上。这样,mask DC的显示平面中的位图即是“掩码”位图。   绘制“透明”位图的实际操作步骤:   1.将位图设备描述表以“SRCINVERT”的方式绘制(BitBlt)到显示设备描述表上;   2.将“掩码”位图设备描述表以“SRCAND”的方式绘制(BitBlt)到显示设备描述表上;   3.再将位图设备描述表以“SRCINVERT”的方式绘制(BitBlt)到显示设备描述表上。这样除“透明色”外的其余位图部分(图像部分)就被绘制到窗口上了。 

解决方案 »

  1.   

    上面取点应该为 dc.GetPixel( x, y );
      

  2.   

    TO  Semigod:
    如何设置bmp的mask,请明示
      

  3.   

    TO qwedcxza(xuxu):
    非常感谢你的这一段帖子,我将试一试,你的那一段摘抄源自何处,请告之
      

  4.   

    hi,应该这样来做。 你看一下我是如何做到透明显示的需要很多bitmap来做,不光是MASK啦,背景可以是任何图象,就是硬往上贴,但是指定的色(keycolor)可以镂空。   SourceDC.FillSolidRect( 0, 0, m_w, m_h, RGB(0, 0, 0) );
       SourceDC.SetTextColor( RGB(255,255,255) );
       SourceDC.DrawText( strTime, &CRect(0, 0, m_w, m_h), DT_CENTER | DT_VCENTER | DT_SINGLELINE );
       ClockDC1.BitBlt(0, 0, m_w, m_h, &ClockDC, 0, 0, NOTSRCCOPY );
       ClockDC1.BitBlt(0, 0, m_w, m_h, &SourceDC, 0, 0, NOTSRCERASE );
       SourceDC.BitBlt( 0, 0, m_w, m_h, &alphaDC, 0, 0, SRCAND );
       SourceDC.BitBlt( 0, 0, m_w, m_h, &ClockDC1, 0, 0, SRCPAINT );
       return pDC->BitBlt( m_xPos, m_yPos, m_w, m_h, &SourceDC, 0, 0, SRCCOPY );
      

  5.   

    用CImageList
    BOOL Create( UINT nBitmapID, int cx, int nGrow, COLORREF crMask );BOOL Create( LPCTSTR lpszBitmapID, int cx, int nGrow, COLORREF crMask );COLORREF crMask 为要去掉的bmp的背景色
      

  6.   

    最后一个参数为你要它透明的颜色,即是那种颜色不会被画出来
    调用方法见最下面void DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap, short xStart,
                               short yStart, COLORREF cTransparentColor)
       {
       BITMAP     bm;
       COLORREF   cColor;
       HBITMAP    bmAndBack, bmAndObject, bmAndMem, bmSave;
       HBITMAP    bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
       HDC        hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
       POINT      ptSize;   hdcTemp = CreateCompatibleDC(hdc);
       SelectObject(hdcTemp, hBitmap);   // Select the bitmap   GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
       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.
       bmBackOld   = SelectObject(hdcBack, bmAndBack);
       bmObjectOld = SelectObject(hdcObject, bmAndObject);
       bmMemOld    = SelectObject(hdcMem, bmAndMem);
       bmSaveOld   = 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 performing a BitBlt
       // from the source bitmap to a monochrome bitmap.
       BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
              SRCCOPY);   // Set the background color of the source DC back to the original
       // color.
       SetBkColor(hdcTemp, cColor);   // Create the inverse of the object mask.
       BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
              NOTSRCCOPY);   // Copy the background of the main DC to the destination.
       BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
              SRCCOPY);   // Mask out the places where the bitmap will be placed.
       BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);   // Mask out the transparent colored pixels on the bitmap.
       BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);   // XOR the bitmap with the background on the destination DC.
       BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);   // Copy the destination to the screen.
       BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
              SRCCOPY);   // Place the original bitmap back into the bitmap sent here.
       BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);   // Delete the memory bitmaps.
       DeleteObject(SelectObject(hdcBack, bmBackOld));
       DeleteObject(SelectObject(hdcObject, bmObjectOld));
       DeleteObject(SelectObject(hdcMem, bmMemOld));
       DeleteObject(SelectObject(hdcSave, bmSaveOld));   // Delete the memory DCs.
       DeleteDC(hdcMem);
       DeleteDC(hdcBack);
       DeleteDC(hdcObject);
       DeleteDC(hdcSave);
       DeleteDC(hdcTemp);
       } 可以这样调用   DrawTransparentBitmap(hdc,         // The destination DC.                         hBitmap,     // The bitmap to be drawn.
                             xPos,        // X coordinate.
                             yPos,        // Y coordinate.
                             0x00FFFFFF); // The color for transparent
                                          // pixels (white, in this
                                          // example).
      

  7.   

    http://263.csdn.net/edyang/download/source/VC/PubUtils.zip基于 MFC 的 CTransparentBmp 实现了透明位图的操作;PubUtilsLab 实例程序演示了如何使用 CTransparentBmp。