作者:BALLOONMAN2002 2004年6月26日
本文拟结合POWERBUILDER语言,简述如何实现图片透明叠加效果,本例参考并改编了以前流传甚广的一篇VB文章,由于时间太久,源出处已无从查找,在此向原作者致意。程序效果图见:
http://blog.csdn.net/images/blog_csdn_net/balloonman2002/17312/r_PIC_OVERLAP.JPG
一、基本思路
程序的原理是先创建两个用于起屏蔽作用的模板DC(装载单色掩码位图,且这二者正好是相反的关系,通过NotSrcCopy实现),分别与源DC和目标DC进行与操作(SrcAnd),这时就能将源DC和目标DC中要透明的区域和不要透明的区域正好各自划分成黑色部分(即象素颜色为0)(具体为源DC中透明区域为黑色,目标DC中不要透明区域为黑色),最后将“操作后”的源DC和目标DC二者再进行“异或”(SrcXor)操作即可实现二者的透明叠加。这里面关键的就是利用0和无论其他什么值(0、1)进行“异或”操作的结果都仍是原值(即仍是0、1),这样就可以保证象素最后XOR的结果是保留了叠加的效果。类似的我们还可以1和其他任何值相“与”的结果仍是原值来实现同样的效果。
这里面关键是创建的起屏蔽作用的模板DC,其就是利用CREATEBITMAP、SETBKCOLOR、BITBLT结合创建的。详见下述代码。
二、具体实现
(一)基本工作
创建一个WINDOW窗口和三个PICTURE控件,分别用于显示源图片、目标图片、合成结果图片。
声明本地外部函数:
Function ulong CreateCompatibleDC(ulong hdc) LIBRARY "gdi32.dll"
Function ulong CreateBitmap(ulong nWidth,ulong nHeight,ulong nPlanes,ulong nBitCount,ulong lpBits) LIBRARY "gdi32.dll"
Function ulong CreateCompatibleBitmap(ulong hdc,ulong nWidth,ulong nHeight) LIBRARY "gdi32.dll"
Function ulong SelectObject(ulong hdc,ulong hObject) LIBRARY "gdi32.dll"
Function ulong SetBkColor(ulong hdc,ulong crColor) LIBRARY "gdi32.dll"
Function ulong BitBlt(ulong hDestDC,ulong x,ulong y,ulong nWidth,ulong nHeight,ulong hSrcDC,ulong xSrc,ulong ySrc,ulong dwRop) LIBRARY "gdi32.dll"
Function ulong DeleteObject(ulong hObject) LIBRARY "gdi32.dll"
Function ulong DeleteDC(ulong hdc) LIBRARY "gdi32.dll"
Function ulong GetDC(ulong hwnd) LIBRARY "user32.dll"
Function ulong ReleaseDC(ulong hwnd,ulong hdc) LIBRARY "user32.dll"
Function ulong LoadImage(ulong hInst,ref string lpsz,ulong un1,ulong n1,ulong n2,ulong un2) LIBRARY "user32.dll" ALIAS FOR "LoadImageA"
(二)具体代码
Long OrigColor
Long OrigMode
Long saveDC
Long maskDC
Long invDC
Long resultDC
Long hSaveBmp
Long hMaskBmp
Long hInvBmp
Long hResultBmp
Long hSavePrevBmp
Long hMaskPrevBmp
Long hInvPrevBmp
Long hDestPrevBmp
Long hsrcdc
Long hDestDC
Long nWidth,nHeight
Long transcolor
long ret,hBitmap
string ls_tmp
hsrcdc = CreateCompatibleDC(getdc(handle(p_1)))
hDestDC = getdc(handle(p_3))
nWidth = UnitsToPixels(p_1.width, XUnitsToPixels!)
nHeight = UnitsToPixels(p_1.height, YUnitsToPixels!)
//这里由于PB默认采用PB UNIT单位,而API处理度量单位采用象素单位因此需要进行单位转换
transcolor = 0
//此处设置要透明的RGB颜色,此处为黑色;
ls_tmp = "srce.bmp"
hBitmap = LoadImage(0, ls_tmp, 0, 0, 0, 16)
saveDC = CreateCompatibleDC(hDestDC)
maskDC = CreateCompatibleDC(hDestDC)
invDC = CreateCompatibleDC(hDestDC)
resultDC = CreateCompatibleDC(hDestDC)
hMaskBmp = CreateBitmap(nWidth, nHeight, 1, 1, 0)
hInvBmp = CreateBitmap(nWidth, nHeight, 1, 1, 0)
hResultBmp = CreateCompatibleBitmap(hDestDC, nWidth, nHeight)
hSaveBmp = CreateCompatibleBitmap(hDestDC, nWidth, nHeight)
ret = SelectObject(hsrcdc, hBitmap)
hSavePrevBmp = SelectObject(saveDC, hSaveBmp)
hMaskPrevBmp = SelectObject(maskDC, hMaskBmp)
hInvPrevBmp = SelectObject(invDC, hInvBmp)
hDestPrevBmp = SelectObject(resultDC, hResultBmp)
OrigColor = SetBkColor(hSrcDC, TransColor)
ret = BitBlt(maskDC, 0, 0, nWidth, nHeight, hSrcDC, 0, 0, 13369376 )
//注:13369376为BitBlt函数的参数-SRCCOPY,表示将源矩形区域直接拷贝到目标矩形区域
TransColor = SetBkColor(hSrcDC, OrigColor)
ret = BitBlt(invDC, 0, 0, nWidth, nHeight, maskDC, 0, 0, 3342344)
//注:3342244为BitBlt函数的参数-NOTSRCCOPY,表示将源矩形区域颜色取反后拷贝到目标矩形区域
ret = BitBlt(resultDC, 0, 0, nWidth, nHeight, hDestDC, 0, 0, 13369376)
ret = BitBlt(resultDC, 0, 0, nWidth, nHeight, maskDC, 0, 0, 8913094)
//注:8913094为BitBlt函数的参数-SRCAND,表示通过使用AND(与)操作符来将源和目标矩形区域内的颜色合并
ret = BitBlt(saveDC, 0, 0, nWidth, nHeight, hSrcDC, 0,0, 13369376)
ret = BitBlt(saveDC, 0, 0, nWidth, nHeight, invDC, 0, 0, 8913094)
ret = BitBlt(resultDC, 0, 0, nWidth, nHeight, saveDC, 0, 0, 6684742)
/注:6684742为BitBlt函数的参数-SRCINVERT,表示通过使用布尔型的XOR(异或)操作符将源和目标矩形区域的颜色合并
ret = BitBlt(hDestDC, 0, 0, nWidth, nHeight, resultDC, 0, 0, 13369376)
ret = SelectObject(saveDC, hSavePrevBmp)
ret = SelectObject(resultDC, hDestPrevBmp)
ret = SelectObject(maskDC, hMaskPrevBmp)
ret = SelectObject(invDC, hInvPrevBmp)
ret = DeleteObject(hSaveBmp)
ret = DeleteObject(hMaskBmp)
ret = DeleteObject(hInvBmp)
ret = DeleteObject(hResultBmp)
ret = DeleteDC(saveDC)
ret = DeleteDC(invDC)
ret = DeleteDC(maskDC)
ret = DeleteDC(resultDC)
ret = ReleaseDC(handle(p_1),hsrcdc)
ret = ReleaseDC(handle(p_3),hDestDC)
至此,图片透明叠加效果全部完成,效果图见:
http://blog.csdn.net/images/blog_csdn_net/balloonman2002/17312/r_PIC_OVERLAP.JPG
如需要进一步资料,请联系QQ:27855043,MSN:WEIYIN2001@MSN.COM
如有不当之处,敬盼您的指点。