透明位图的显示Word下载.docx
- 文档编号:999458
- 上传时间:2023-04-30
- 格式:DOCX
- 页数:11
- 大小:57.82KB
透明位图的显示Word下载.docx
《透明位图的显示Word下载.docx》由会员分享,可在线阅读,更多相关《透明位图的显示Word下载.docx(11页珍藏版)》请在冰点文库上搜索。
intnYOriginSrc,//源Y起点
intnWidthSrc,//源宽度
intnHeightSrc,//源高度
UINTcrTransparent//透明色,COLORREF类型
);
使用示例:
CBitmapFootballBMP;
FootballBMP.LoadBitmap(IDB_FOOTBALLBMP);
CDCImageDC;
ImageDC.CreateCompatibleDC(pDC);
CBitmap*pOldImageBMP=ImageDC.SelectObject(&
FootballBMP);
TransparentBlt(pDC->
m_hDC,0,0,218,199,ImageDC.m_hDC,0,0,218,199,RGB(0,0,0xff));
ImageDC.SelectObject(pOldImageBMP);
二、实现TransparentBlt函数
为了理解具有透明色位图的绘制过程,我们来亲手建立一个具有同TransparentBlt功能一致的实验函数,称之为TransparentBlt2。
实验素材:
有两张位图:
bk.bmp是背景位图,football.bmp包含透明区域,透明色为蓝色RGB(0,0,0xff)
实验目的:
以bk.bmp为背景,将football.bmp绘制到背景中,形成如下的最终效果图。
2.1透明位图绘制原理
假设football.bmp->
载入HBITMAPhImageBMP->
选入HDChImageDC
2.1.1生成足球的单色掩码位图,透明区域为白色(全1),非透明区域为黑色(全0)
HBITMAPhMaskBMP=CreateBitmap(nWidthDest,nHeightDest,1,1,NULL);
//建立单色位图
SetBkColor(hImageDC,RGB(0,0,0xff));
//设置背景色为蓝色
BitBlt(hMaskDC,0,0,nWidthDest,nHeightDest,hImageDC,0,0,SRCCOPY);
//拷贝到hMaskDC
这样足球位图中蓝色区域在掩码位图中成了白色,其它区域为黑色,此时hMaskBMP如下图:
(图一)
2.1.2设置背景色为黑色,前景色为白色,将掩码位图(图一)与足球位图相"
与"
SetBkColor(hImageDC,RGB(0,0,0));
SetTextColor(hImageDC,RGB(255,255,255));
BitBlt(hImageDC,0,0,nWidthDest,nHeightDest,hMaskDC,0,0,SRCAND);
这样,掩码位图中背景色(黑色)的区域在hImageBMP中被保留,前景色(白色)的部分变为黑色。
此时hImageBMP如下图:
(图二)
2.1.3设置背景色为白色,前景色为黑色,将掩码位图(图一)与背景进行“与”运算
SetBkColor(hdcDest,RGB(255,255,255));
SetTextColor(hdcDest,RGB(0,0,0));
BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthDest,nHeightDest,hMaskDC,0,0,SRCAND);
掩码中白色区域(数据与1相“与”结果不变)使背景保持不变,黑色区域变成黑色,此时背景显示如下:
(图三)
2.1.4将hImageBMP(图二)与背景(图三)进行“或”运算
BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthDest,nHeightDest,hImageDC,0,0,SRCPAINT);
这样就将足球绘制到背景上了。
2.2TransparentBlt2函数全部实现代码
voidTransparentBlt2(HDChdcDest,//目标DC
intnXOriginDest,//目标X偏移
intnYOriginDest,//目标Y偏移
intnWidthDest,//目标宽度
intnHeightDest,//目标高度
HDChdcSrc,//源DC
intnXOriginSrc,//源X起点
intnYOriginSrc,//源Y起点
intnWidthSrc,//源宽度
intnHeightSrc,//源高度
UINTcrTransparent//透明色,COLORREF类型
)
{
HBITMAPhOldImageBMP,hImageBMP=CreateCompatibleBitmap(hdcDest,nWidthDest,nHeightDest);
//创建兼容位图
HBITMAPhOldMaskBMP,hMaskBMP=CreateBitmap(nWidthDest,nHeightDest,1,1,NULL);
//创建单色掩码位图
HDChImageDC=CreateCompatibleDC(hdcDest);
HDChMaskDC=CreateCompatibleDC(hdcDest);
hOldImageBMP=(HBITMAP)SelectObject(hImageDC,hImageBMP);
hOldMaskBMP=(HBITMAP)SelectObject(hMaskDC,hMaskBMP);
//将源DC中的位图拷贝到临时DC中
if(nWidthDest==nWidthSrc&
&
nHeightDest==nHeightSrc)
BitBlt(hImageDC,0,0,nWidthDest,nHeightDest,hdcSrc,nXOriginSrc,nYOriginSrc,SRCCOPY);
else
StretchBlt(hImageDC,0,0,nWidthDest,nHeightDest,
hdcSrc,nXOriginSrc,nYOriginSrc,nWidthSrc,nHeightSrc,SRCCOPY);
//设置透明色
SetBkColor(hImageDC,crTransparent);
//生成透明区域为白色,其它区域为黑色的掩码位图
BitBlt(hMaskDC,0,0,nWidthDest,nHeightDest,hImageDC,0,0,SRCCOPY);
//生成透明区域为黑色,其它区域保持不变的位图
SetBkColor(hImageDC,RGB(0,0,0));
SetTextColor(hImageDC,RGB(255,255,255));
BitBlt(hImageDC,0,0,nWidthDest,nHeightDest,hMaskDC,0,0,SRCAND);
//透明部分保持屏幕不变,其它部分变成黑色
SetBkColor(hdcDest,RGB(255,255,255));
SetTextColor(hdcDest,RGB(0,0,0));
BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthDest,nHeightDest,hMaskDC,0,0,SRCAND);
//"
或"
运算,生成最终效果
BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthDest,nHeightDest,hImageDC,0,0,SRCPAINT);
//清理、恢复
SelectObject(hImageDC,hOldImageBMP);
DeleteDC(hImageDC);
SelectObject(hMaskDC,hOldMaskBMP);
DeleteDC(hMaskDC);
DeleteObject(hImageBMP);
DeleteObject(hMaskBMP);
}
2.3TransparentBlt的另外一个版本:
TransparentBltU
TransparentBltU是ChristianGraus在WinDEV发表的一个函数,功能与TransparentBlt一致,以下是全部实现代码:
boolTransparentBltU(
HDCdcDest,//handletoDestDC
intnXOriginDest,//x-coordofdestinationupper-leftcorner
intnYOriginDest,//y-coordofdestinationupper-leftcorner
intnWidthDest,//widthofdestinationrectangle
intnHeightDest,//heightofdestinationrectangle
HDCdcSrc,//handletosourceDC
intnXOriginSrc,//x-coordofsourceupper-leftcorner
intnYOriginSrc,//y-coordofsourceupper-leftcorner
intnWidthSrc,//widthofsourcerectangle
intnHeightSrc,//heightofsourcerectangle
UINTcrTransparent//colortomaketransparent
)
if(nWidthDest<
1)returnfalse;
if(nWidthSrc<
if(nHeightDest<
if(nHeightSrc<
HDCdc=CreateCompatibleDC(NULL);
HBITMAPbitmap=CreateBitmap(nWidthSrc,nHeightSrc,1,GetDeviceCaps(dc,
BITSPIXEL),NULL);
if(bitmap==NULL)
{
DeleteDC(dc);
returnfalse;
}
HBITMAPoldBitmap=(HBITMAP)SelectObject(dc,bitmap);
if(!
BitBlt(dc,0,0,nWidthSrc,nHeightSrc,dcSrc,nXOriginSrc,
nYOriginSrc,SRCCOPY))
SelectObject(dc,oldBitmap);
DeleteObject(bitmap);
HDCmaskDC=CreateCompatibleDC(NULL);
HBITMAPmaskBitmap=CreateBitmap(nWidthSrc,nHeightSrc,1,1,NULL);
if(maskBitmap==NULL)
DeleteDC(maskDC);
HBITMAPoldMask=(HBITMAP)SelectObject(maskDC,maskBitmap);
SetBkColor(maskDC,RGB(0,0,0));
SetTextColor(maskDC,RGB(255,255,255));
BitBlt(maskDC,0,0,nWidthSrc,nHeightSrc,NULL,0,0,BLACKNESS))
SelectObject(maskDC,oldMask);
DeleteObject(maskBitmap);
SetBkColor(dc,crTransparent);
BitBlt(maskDC,0,0,nWidthSrc,nHeightSrc,dc,0,0,SRCINVERT);
SetBkColor(dc,RGB(0,0,0));
SetTextColor(dc,RGB(255,255,255));
BitBlt(dc,0,0,nWidthSrc,nHeightSrc,maskDC,0,0,SRCAND);
HDCnewMaskDC=CreateCompatibleDC(NULL);
HBITMAPnewMask;
newMask=CreateBitmap(nWidthDest,nHeightDest,1,
GetDeviceCaps(newMaskDC,BITSPIXEL),NULL);
if(newMask==NULL)
DeleteDC(newMaskDC);
SetStretchBltMode(newMaskDC,COLORONCOLOR);
HBITMAPoldNewMask=(HBITMAP)SelectObject(newMaskDC,newMask);
StretchBlt(newMaskDC,0,0,nWidthDest,nHeightDest,maskDC,0,0,
nWidthSrc,nHeightSrc,SRCCOPY);
HDCnewImageDC=CreateCompatibleDC(NULL);
HBITMAPnewImage=CreateBitmap(nWidthDest,nHeightDest,1,
if(newImage==NULL)
HBITMAPoldNewImage=(HBITMAP)SelectObject(newImageDC,newImage);
StretchBlt(newImageDC,0,0,nWidthDest,nHeightDest,dc,0,0,nWidthSrc,
nHeightSrc,SRCCOPY);
BitBlt(dcDest,nXOriginDest,nYOriginDest,nWidthDest,nHeightDest,
newMaskDC,0,0,SRCAND);
newImageDC,0,0,SRCPAINT);
SelectObject(newImageDC,oldNewImage);
DeleteDC(newImageDC);
SelectObject(newMaskDC,oldNewMask);
DeleteObject(newImage);
DeleteObject(newMask);
returntrue;
说明:
本文提供的TransparentBlt2函数旨在说明透明位图的显示原理,在Windows2000以上环境实际运用中建议使用现成的TransparentBlt函数来绘制透明位图。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 透明 位图 显示
![提示](https://static.bingdoc.com/images/bang_tan.gif)