`
阿尔萨斯
  • 浏览: 4176891 次
社区版块
存档分类
最新评论

图片镂空算法集合[图]

 
阅读更多
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>

在开发界面及棋牌游戏过程中,需要很多镂空的图片,而且图片形式一般比较固定.

所以封装了几种常见的镂空方法.

1. 用于没有掩码图,只有指定透明色,不进行伸缩

voidDrawTransBitmap(HDChdcDest, //目标DC
intnXOriginDest,//目标X偏移
intnYOriginDest,//目标Y偏移
intnWidthDest, //目标宽度
intnHeightDest,//目标高度
HDChdcSrc, //源DC
intnXOriginSrc,//源X起点
intnYOriginSrc, //源Y起点
COLORREF crTransparent//透明色,COLORREF类型
);

适用图片:

2. 用于没有掩码图,只有指定透明色,可以进行伸缩

voidDrawTransBitmap(HDChdcDest, //目标DC
intnXOriginDest,//目标X偏移
intnYOriginDest,//目标Y偏移
intnWidthDest, //目标宽度
intnHeightDest,//目标高度
HDChdcSrc, //源DC
intnXOriginSrc,//源X起点
intnYOriginSrc, //源Y起点
intnWidthSrc, //源宽度
intnHeightSrc, //源高度
COLORREF crTransparent//透明色,COLORREF类型
);

适用图片:同1,可拉伸

3.指定掩码图,和掩码图属于不同图片

voidDrawTransBitmap(HDChdcDest, //目标DC
intnXOriginDest, //目标X偏移
intnYOriginDest, //目标Y偏移
intnWidthDest, //目标宽度
intnHeightDest, //目标高度
HDChdcSrc, //源DC
HDChdcMask, //掩码DC
intnXOriginSrc, //源X起点
intnYOriginSrc, //源Y起点
COLORREFcrTransparent//透明色,COLORREF类型
);

适用图片:

4.指定图片和掩码图同属于一张图片

voidDrawTransBitmap(HDChDC,
intnPosX,
intnPosY,
intnCX,
intnCY,
HBITMAPhObj
);

适用图片:

5.得到位图HRGN

HRGNCreateBitmapRgn(intnWidth,intnHeight,HBITMAPhbmp,COLORREFcTrans);

适用图片:

以下是完整代码

//用于没有掩码图,只有指定透明色,不进行伸缩
voidCCommon::DrawTransBitmap(HDChdcDest,//目标DC
intnXOriginDest,//目标X偏移
intnYOriginDest,//目标Y偏移
intnWidthDest,//目标宽度
intnHeightDest,//目标高度
HDChdcSrc,//源DC
intnXOriginSrc,//源X起点
intnYOriginSrc,//源Y起点
COLORREFcrTransparent//透明色,COLORREF类型
)
...
...{
HBITMAPhOldImageBMP,hImageBMP
=CreateCompatibleBitmap(hdcDest,nWidthDest,nHeightDest);//创建兼容位图
HBITMAPhOldMaskBMP,hMaskBMP=CreateBitmap(nWidthDest,nHeightDest,1,1,NULL);//创建单色掩码位图
HDChImageDC=CreateCompatibleDC(hdcDest);//临时DC
HDChMaskDC=CreateCompatibleDC(hdcDest);//临时掩码DC
hOldImageBMP=(HBITMAP)SelectObject(hImageDC,hImageBMP);
hOldMaskBMP
=(HBITMAP)SelectObject(hMaskDC,hMaskBMP);

//将源DC中的位图拷贝到临时DC中,源DC已经载入位图
BitBlt(hImageDC,0,0,nWidthDest,nHeightDest,hdcSrc,nXOriginSrc,nYOriginSrc,SRCCOPY);

//设置临时DC的透明色
SetBkColor(hImageDC,crTransparent);

//生成透明区域为白色,其它区域为黑色的临时掩码DC的掩码位图
//位图来自临时DC
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);
}



//用于没有掩码图,只有指定透明色,可以进行伸缩
voidCCommon::DrawTransBitmap(HDChdcDest,//目标DC
intnXOriginDest,//目标X偏移
intnYOriginDest,//目标Y偏移
intnWidthDest,//目标宽度
intnHeightDest,//目标高度
HDChdcSrc,//源DC
intnXOriginSrc,//源X起点
intnYOriginSrc,//源Y起点
intnWidthSrc,//源宽度
intnHeightSrc,//源高度
COLORREFcrTransparent//透明色,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(0xff,0xff,0xff));
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);

}



指定掩码图,和掩码图属于不同图片
voidCCommon::DrawTransBitmap(HDChdcDest,//目标DC
intnXOriginDest,//目标X偏移
intnYOriginDest,//目标Y偏移
intnWidthDest,//目标宽度
intnHeightDest,//目标高度
HDChdcSrc,//源DC
HDChdcMask,
intnXOriginSrc,//源X起点
intnYOriginSrc,//源Y起点
COLORREFcrTransparent//透明色,COLORREF类型
)
...
...{

HBITMAPhOldImageBMP,hImageBMP
=CreateCompatibleBitmap(hdcDest,nWidthDest,nHeightDest);//创建兼容位图
HDChImageDC=CreateCompatibleDC(hdcDest);//临时DC
hOldImageBMP=(HBITMAP)SelectObject(hImageDC,hImageBMP);

//将源DC中的位图拷贝到临时DC中,源DC已经载入位图
BitBlt(hImageDC,0,0,nWidthDest,nHeightDest,hdcSrc,nXOriginSrc,nYOriginSrc,SRCCOPY);
//设置临时DC的透明色
SetBkColor(hImageDC,crTransparent);
//生成透明区域为黑色,其它区域保持不变的位图
SetBkColor(hImageDC,RGB(0,0,0));
SetTextColor(hImageDC,RGB(
255,255,255));
BitBlt(hImageDC,
0,0,nWidthDest,nHeightDest,hdcMask,0,0,SRCAND);
//透明部分保持屏幕不变,其它部分变成黑色
SetBkColor(hdcDest,RGB(255,255,255));
SetTextColor(hdcDest,RGB(
0,0,0));
BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthDest,nHeightDest,hdcMask,
0,0,SRCAND);
//"或"运算,生成最终效果
BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthDest,nHeightDest,hImageDC,0,0,SRCPAINT);
//清理、恢复
SelectObject(hImageDC,hOldImageBMP);
DeleteDC(hImageDC);
DeleteObject(hImageBMP);
}


指定图片和掩码图同属于一张图片
voidCCommon::DrawTransBitmap(HDChDC,intnPosX,intnPosY,intnCX,intnCY,HBITMAPhObj)
...
...{
HDChMemDC
=CreateCompatibleDC(hDC);
HBITMAPhOldBMP
=(HBITMAP)::SelectObject(hMemDC,hObj);
BitBlt(hDC,nPosX,nPosY,nCX,nCY,hMemDC,nCX,
0,SRCAND);
BitBlt(hDC,nPosX,nPosY,nCX,nCY,hMemDC,
0,0,SRCPAINT);
SelectObject(hMemDC,hOldBMP);
DeleteDC(hMemDC);

}


HRGNCCommon::CreateBitmapRgn(
intnWidth,intnHeight,HBITMAPhbmp,COLORREFTransColor)
...
...{

HDChmemDC;
//创建与传入DC兼容的临时DC
hmemDC=::CreateCompatibleDC(NULL);

HBITMAPhOldBmp
=(HBITMAP)::SelectObject(hmemDC,hbmp);


//创建总的窗体区域,初始region为0
HRGNhrgn;
hrgn
=::CreateRectRgn(0,0,0,0);


inty;
for(y=0;ynHeight;y++)
...
...{
HRGNrgnTemp;
//保存临时region

intiX=0;
do
...
...{
//跳过透明色找到下一个非透明色的点.
while(iXnWidth&&::GetPixel(hmemDC,iX,y)==TransColor)
iX
++;

//记住这个起始点
intiLeftX=iX;

//寻找下个透明色的点
while(iXnWidth&&::GetPixel(hmemDC,iX,y)!=TransColor)
++iX;

//创建一个包含起点与重点间高为1像素的临时“region”
rgnTemp=::CreateRectRgn(iLeftX,y,iX,y+1);

//合并到主"region".
CombineRgn(hrgn,hrgn,rgnTemp,RGN_OR);

//删除临时"region",否则下次创建时和出错
::DeleteObject(rgnTemp);
}
while(iXnWidth);
iX
=0;
}



::SelectObject(hmemDC,hOldBmp);
::DeleteDC(hmemDC);

returnhrgn;

}




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics