分类: C/C++
2008-08-06 14:33:12
VC截图及用AlphaBlend实现半透明位图 |
来源: 索尔网络游戏工作室 作者:索尔网络 发表日期: 2007-5-19 10:07:37 |
Requirements:
Windows NT/2000/XP: Included in Windows 2000 and later.
Windows 95/98/Me: Included in Windows 98 and later.
Header: Declared in Wingdi.h; include Windows.h.
Library: Included as a resource in Msimg32.dll.
示例:
void CTestDlg::SaveBitmap(CDC* pDC,CRect rect,CString filename)
{
CDC* memDC=new CDC;
memDC->CreateCompatibleDC(pDC);
CBitmap* bmp=new CBitmap;
bmp->CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
CBitmap* oldbitmap=memDC->SelectObject(bmp);
//此时的bmp就相当于一张桌布,在memDC中画线etc都是画在这张桌布上
if(!memDC->BitBlt(0,0,rect.Width(),rect.Height(),pDC,0,0,SRCCOPY))
{
AfxMessageBox("BitBlt Error!");
return;
}
memDC->Ellipse(0,0,100,100);
memDC->SelectObject(oldbitmap);
BITMAPINFO bi;
bi.bmiHeader.biSize=sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth=rect.Width();
bi.bmiHeader.biHeight=rect.Height();
bi.bmiHeader.biPlanes=1;
bi.bmiHeader.biBitCount=16;
bi.bmiHeader.biCompression=BI_RGB;
bi.bmiHeader.biSizeImage=0;
bi.bmiHeader.biXPelsPerMeter=0;
bi.bmiHeader.biYPelsPerMeter=0;
bi.bmiHeader.biClrUsed=0;
bi.bmiHeader.biClrImportant=0;
int bitsize=rect.Width()*rect.Height()*2;
BYTE* bits=new BYTE[bitsize];
::GetDIBits(memDC->m_hDC,*bmp,0,rect.Height(),bits,&bi,DIB_RGB_COLORS);
BITMAPFILEHEADER bf;
bf.bfType=(int)'M'*256+'B';
bf.bfSize=bitsize;//sizeof(bf);
bf.bfOffBits=sizeof(bi.bmiHeader)+sizeof(bf);
bf.bfReserved1=0;
bf.bfReserved2=0;
CFile f(filename,CFile::modeCreate|CFile::modeWrite);
f.Write(&bf,sizeof(bf));//注意是先写bf,再写bi
f.Write(&bi,sizeof(bi));
f.Write(bits,bitsize);
f.Close();
delete[] bits;
delete bmp;
delete memDC;
}
将memDC上的位图半透明覆盖到pDC上
BLENDFUNCTION bm;
bm.BlendOp=AC_SRC_OVER;
bm.BlendFlags=0;
bm.SourceConstantAlpha=100;
bm.AlphaFormat=0;
AlphaBlend(pDC->m_hDC,0,0,rect.Width(),rect.Height(),memDC->m_hDC,0,0,rect.Width(),rect.Height(),bm); |