透明位图绘制方法在网上见得很多,多数是采用事先做好一个mask图,这方法优点是速度快,但就是太麻烦,灵活性差。
任意指定透明色,当然经常也要用到,为此,api提供了一个函数transparentblt,可这个函数,非常让人遗憾,vb的api浏览器中不带它是有道理的,因为,它在win98下有严重内存漏洞,你若有98系统,可试一下:
for i=1 to 20000
transparentblt ....
next
同样的图片,在我的xp下16毫秒可完成,但在98下用了14秒,而且,提示系统资源不足,当机了!
下面我写了一个函数就是可以代替transparentblt的一种方法,速度当然会慢些,但在任何系统下都可放心使用。
private type bitmap
bmtype as long
bmwidth as long
bmheight as long
bmwidthbytes as long
bmplanes as integer
bmbitspixel as integer
bmbits as long
end type
private declare function getcurrentobject lib "gdi32" (byval hdc as long, byval uobjecttype as long) as long
private declare function getobj lib "gdi32" alias "getobjecta" (byval hobject as long, byval ncount as long, lpobject as any) as long
private declare function setbkcolor lib "gdi32" (byval hdc as long, byval crcolor as long) as long
private declare function createbitmap lib "gdi32" (byval nwidth as long, byval nheight as long, byval nplanes as long, byval nbitcount as long, lpbits as any) as long
private declare function getpixel lib "gdi32" (byval hdc as long, byval x as long, byval y as long) as long
private declare function bitblt lib "gdi32" (byval hdestdc as long, byval x as long, byval y as long, byval nwidth as long, byval nheight as long, byval hsrcdc as long, byval xsrc as long, byval ysrc as long, byval dwrop as long) as long
private declare function stretchblt lib "gdi32" (byval hdc as long, byval x as long, byval y as long, byval nwidth as long, byval nheight as long, byval hsrcdc as long, byval xsrc as long, byval ysrc as long, byval nsrcwidth as long, byval nsrcheight as long, byval dwrop as long) as long
public function tranblt(desthdc as long, x as long, y as long, w as long, h as long, srchdcorbmp as long, optional srcx as long, optional srcy as long, optional srcw as long, optional srch as long, optional tc as long = -1, optional isbmp as boolean) as long
'srchdcorbmp参数 传入的可以是hdc也可以是bmp对象的handle,
'isbmp参数 为真时srchdcorbmp代表bmp对象的handle,为假时代表hdc
'返回值 成功时返回透明色,不成功时返回-1
dim thdc(3) as memhdc
dim j as long, oc as long, i as long, bm as bitmap, cc as long, newdc as long
dim sw as long, sh as long, sbmp as long, shdc as long, obm as long, newx as long, newy as long
if desthdc = 0 or srchdcorbmp = 0 or w = 1 and h = 1 then goto fail
if isbmp then '若传入的是bmp句柄,需为其创建一个临时dc
sbmp = srchdcorbmp
thdc(3) = newmyhdc(desthdc, 0, 0, srchdcorbmp)
shdc = thdc(3).hdc
else
shdc = srchdcorbmp
if srcw = 0 then sbmp = getcurrentobject(shdc, 7)
end if
if shdc = 0 or sbmp = 0 then goto fail
if srcw = 0 then '若没有提供源图大小,需取得整个源图大小
getobj sbmp, len(bm), bm
sw = bm.bmwidth - srcx
sh = bm.bmheight - srcy
else
sw = srcw
sh = srch
end if
if sw < 1 or sh < 1 then goto fail
if tc = -1 then
cc = getpixel(shdc, srcx, srcy) '将左上角第一个像素作为源图背景色,用于透明
else
cc = tc
end if
if w <> sw or h <> sh then
thdc(2) = newmyhdc(desthdc, w, h)
stretchblt thdc(2).hdc, 0, 0, w, h, shdc, srcx, srcy, sw, sh, vbsrccopy
'先将源图缩放,下面步骤就一样了。
newdc = thdc(2).hdc
else
newdc = shdc
newx = srcx
newy = srcy
end if
bitblt desthdc, x, y, w, h, newdc, newx, newy, vbsrcinvert
'将源图先反色(xor)绘入目标图,若源图背景为黑色,此步可省
'下面是制作mask图的方法
i = createbitmap(w, h, 1, 1, byval 0&) '建立单色位图
thdc(0) = newmyhdc(desthdc, 0, 0, i) '为单色图建立新dc,并选入
thdc(1) = newmyhdc(desthdc, w, h) '另建一个彩色图及dc,用于存放mask图
oc = setbkcolor(newdc, cc) '将源图背景色改为透明色
bitblt thdc(0).hdc, 0, 0, w, h, newdc, newx, newy, vbsrccopy
'先将源图绘入单色dc,由此产生只有正反的mask图,背景色(透明色)为黑,其它为白
setbkcolor newdc, oc '恢复源图背景色,不是必须的,但这是个好习惯。
bitblt thdc(1).hdc, 0, 0, w, h, thdc(0).hdc, 0, 0, vbsrccopy
'单色dc必须复制进彩色dc才能进行后面的的and运算
'mask图完成,并已放入彩色dc
bitblt desthdc, x, y, w, h, thdc(1).hdc, 0, 0, vbsrcand '标准透明绘图:选将mask图用and运算绘入,
bitblt desthdc, x, y, w, h, newdc, newx, newy, vbsrcinvert '再将源图以反色(xor)绘入一次
delmyhdc thdc(0)
delmyhdc thdc(1)
if thdc(2).hdc <> 0 then delmyhdc thdc(2)
if thdc(3).hdc <> 0 then delmyhdc thdc(3)
tranblt = cc
exit function
fail:
if thdc(3).hdc <> 0 then delmyhdc thdc(3)
tranblt = -1
end function
private function newmyhdc(dhdc as long, w as long, h as long, optional bm as long) as memhdc
with newmyhdc
.hdc = createcompatibledc(dhdc)
if bm = 0 then
.bmp = createcompatiblebitmap(dhdc, w, h)
else
.bmp = bm
end if
.obm = selectobject(.hdc, .bmp)
end with
end function
private function delmyhdc(myhdc as memhdc, optional nobmp as boolean) as memhdc
with myhdc
if .hdc <> 0 and .obm <> 0 then selectobject .hdc, .obm
if nobmp = false and .bmp <> 0 then deleteobject .bmp
if .hdc <> 0 then deletedc .hdc
end with
end function
private sub command1_click()
tranblt picture1.hdc, 0, 0, image1.width, image1.height, image1.picture.handle, , , , , , true
end sub
private sub form_load()
me.scalemode = 3
end sub
本篇中的公用函数newmyhdc、delmyhdc及相关结构与api声明,可在以下文章中找到
http://blog.csdn.net/homezj/archive/2005/04/14/348001.aspx
新闻热点
疑难解答