如何进行位图的透明色拷贝

visual 2000-08-14 11:34:00
在direc x 中,有bltfast和setcolorkey实现位图块的透明色拷贝、传输。请问在mfc中如何实现?
如果哪位对directx不是很熟悉,我的意思是我在一幅背景为黑色的bmp上画了一个红色、蓝色、白色混合的房子(不规则图形),现在我想把它显示在view上,但只想把房子显示出来,而不想要房子的背景黑色,从而实现透明色的位图块的传输。我查了一下BitBlt和StretchBlt,好像并没有这方面的资料。
请各位指点,如有源码,望赐教。
谢!
...全文
120 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
visual 2000-08-14
  • 打赏
  • 举报
回复
谢谢!
huntout 2000-08-14
  • 打赏
  • 举报
回复
//////////////////////////////////////////////////////////////
// Function : Put transparent bitmap
// Input : nPer = 0 ; the whole image
// = 1~nTotal ; 1st to last piece of bitmap
// nTotal ; number of pieces
// bHorz ; bitmap is horizonal
// color : 須透明的顏色
//////////////////////////////////////////////////////////////
void PutBitmap(CDC* pDC, int x, int y, CBitmap* pBitmap,
int nPer, int nTotal, BOOL bHorz, COLORREF color)
{
COLORREF oldColor;
CBitmap bmAndBack, bmAndObject, bmAndMem, bmSave;
CBitmap *old1, *old2, *old3, *old4, *old5;
CDC memDC, backDC, objectDC, saveDC, pieceDC;
BITMAP bm;
int xSrc, ySrc, nWidth, nHeight;

// Get the bitmap size and format.
pBitmap->GetBitmap(&bm);
if (nPer <= 0 || nTotal <= 0 || nPer > nTotal)
{
xSrc = 0;
ySrc = 0;
nWidth = bm.bmWidth;
nHeight = bm.bmHeight;
}
else
{
if (bHorz)
{
xSrc = bm.bmWidth * (nPer - 1) / nTotal;
ySrc = 0;
nWidth = bm.bmWidth / nTotal;
nHeight = bm.bmHeight;
}
else
{
xSrc = 0;
ySrc = bm.bmHeight * (nPer - 1) / nTotal;
nWidth = bm.bmWidth;
nHeight = bm.bmHeight / nTotal;
}
}

// Create device contexts for the various objects we'll blit.
memDC.CreateCompatibleDC (pDC);
backDC.CreateCompatibleDC (pDC);
objectDC.CreateCompatibleDC (pDC);
saveDC.CreateCompatibleDC (pDC);
pieceDC.CreateCompatibleDC (pDC);

// Create four bitmaps, two mono-planed and two the same as the source.
bmAndBack.CreateBitmap (nWidth, nHeight, 1, 1, NULL);
bmAndObject.CreateBitmap (nWidth, nHeight, 1, 1, NULL);
bmAndMem.CreateCompatibleBitmap (pDC, nWidth, nHeight);
bmSave.CreateCompatibleBitmap (pDC, nWidth, nHeight);

// Select the bitmaps into their device contexts and save the
// old bitmaps so we can select them back in at the end.
old1 = backDC.SelectObject (&bmAndBack);
old2 = objectDC.SelectObject (&bmAndObject);
old3 = memDC.SelectObject (&bmAndMem);
old4 = saveDC.SelectObject (&bmSave);
old5 = pieceDC.SelectObject (pBitmap);

// Copy the bitmap to save it.
saveDC.BitBlt (0, 0, nWidth, nHeight, &pieceDC, xSrc, ySrc, SRCCOPY);

// Set the background color to mask and create a mask.
oldColor = pieceDC.SetBkColor (color);
objectDC.BitBlt (0, 0, nWidth, nHeight, &pieceDC, xSrc, ySrc, SRCCOPY);
pieceDC.SetBkColor (oldColor);

// Mask the source bitmap itself.
backDC.BitBlt (0, 0, nWidth, nHeight, &objectDC, 0, 0, NOTSRCCOPY);

// Get the data from the screen.
memDC.BitBlt (0, 0, nWidth, nHeight, pDC, x, y, SRCCOPY);

// Mask the data we just got from the screen.
memDC.BitBlt (0, 0, nWidth, nHeight, &objectDC, 0, 0, SRCAND);

// Mask the image.
pieceDC.BitBlt (xSrc, ySrc, nWidth, nHeight, &backDC, 0, 0, SRCAND);

// OR the image and the data we just got from the screen.
memDC.BitBlt (0, 0, nWidth, nHeight, &pieceDC, xSrc, ySrc, SRCPAINT);

// Blit the newly masked and combined image to the screen.
pDC->BitBlt (x, y, nWidth, nHeight, &memDC, 0, 0, SRCCOPY);

// Restore the image by blitting the saved data back into it.
pieceDC.BitBlt (xSrc, ySrc, nWidth, nHeight, &saveDC, 0, 0, SRCCOPY);

// Select the old bitmaps
backDC.SelectObject (old1);
objectDC.SelectObject (old2);
memDC.SelectObject (old3);
saveDC.SelectObject (old4);
pieceDC.SelectObject (old5);

// Delete the bitmaps we created.
bmAndBack.DeleteObject ();
bmAndObject.DeleteObject ();
bmAndMem.DeleteObject ();
bmSave.DeleteObject ();

// The device contexts will be destroyed in their destructors.
}

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧