您的位置:程序门 -> vb -> 基础类



大家好,谁可以告诉我图片透明度处理方法!谢了!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


大家好,谁可以告诉我图片透明度处理方法!谢了!
发表于:2008-01-18 10:58:19 楼主
大家好,谁可以告诉我图片透明度处理方法!谢了!目前在做一个项目用vb写一个处理图片的程序,我在image控件中有一张图片,我想对该图片进行透明度处理,有什么好的方法吗?谢谢!
发表于:2008-01-18 11:08:041楼 得分:0
http://tech.sina.com.cn/c/2002-01-29/10968.html
发表于:2008-01-18 11:11:242楼 得分:0
图片透明合成效果       http://www.newasp.net/code/vb/715.html
发表于:2008-01-18 11:18:183楼 得分:0
你没听明白我的意思,我是不是要个背景透明的.我是想和photoshop似的,可以调整图片的透明度,不是背景的!谢谢了!
发表于:2008-01-18 11:28:004楼 得分:0
http://blog.csdn.net/laviewpbt/archive/2006/05/26/756464.aspx

模拟photoshop中图层混合模式          


http://www.vbgood.com/viewthread.php?tid=53287&extra=page%3d1
发表于:2008-01-18 11:30:375楼 得分:0
alpha运算
发表于:2008-01-18 14:08:116楼 得分:0
真的没人知道吗?!
发表于:2008-01-18 14:53:537楼 得分:0
mark
发表于:2008-01-21 01:04:388楼 得分:0
用alphabland这个api可以实现你需要的效果,msdn上有详细的例子可以参考
发表于:2008-01-23 12:51:199楼 得分:0
下面的代码希望对你有用

vbscript code
option explicit '------------------ structure ------------------------------------------------ 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 type mycolor24 b as byte g as byte r as byte end type type mycolor32 b as byte g as byte r as byte a as byte end type '------------------ api ------------------------------------------------------ declare function getbitmapbits lib "gdi32" (byval hbitmap as long, byval dwcount as long, lpbits as any) as long declare function setbitmapbits lib "gdi32" (byval hbitmap as long, byval dwcount as long, lpbits as any) as long declare function getobjectapi& lib "gdi32" alias "getobjecta" (byval hobject as long, byval ncount as long, lpobject as any) '----------------- global function ------------------------------------------- '功能:将前景位图经掩模运算后与背景混合,然后按整体透明度复制到目的位图。 '引用:(type)bitmap,(type)mycolor24,(api)getbitmapbits,(api)setbitmapbits,(api)getobjectapi '说明:如果未指定hfsourcebmp(前景),掩模将失去作用;如果未指定hmaskbmp(掩模),前景将完全覆盖背景;如果未指定alpharate(透明度),将不做整体透明处理 '参数: ' hdestbmp 目的位图句柄 ' dx 目的x坐标 ' dy 目的y坐标 ' dw 目的宽度 ' dh 目的高度 ' hbsourcebmp 背景位图句柄 ' bsx 背景x坐标 ' bsy 背景y坐标 ' hfsourcebmp 前景位图句柄 ' fsx 前景x坐标 ' fsy 前景y坐标 ' hmaskbmp 掩模位图句柄 ' mx 掩模x坐标 ' my 掩模y坐标 ' alpha 整体透明度 sub abimage(byval hdest as long, byval dx as long, byval dy as long, byval dw as long, byval dh as long, byval hbsource as long, byval bsx as long, byval bsy as long, byval hfsource as long, byval fsx as long, byval fsy as long, byval hmask as long, byval mx as long, byval my as long, byval alpharate as byte) '各位图的信息 dim dinfo as bitmap, bsinfo as bitmap, fsinfo as bitmap, minfo as bitmap '各位图的数据 dim ddata() as mycolor24, bsdata() as mycolor24, fsdata() as mycolor24, mdata() as mycolor24 dim ddata32() as mycolor32, bsdata32() as mycolor32, fsdata32() as mycolor32, mdata32() as mycolor32 'x,y=循环变量;w,h=循环次数;offsetx,offsety=相对于目的位图坐标偏移 dim x as long, y as long, w as long, h as long, offsetx as long, offsety as long '混合比率 dim rate as single '得到各位图的信息 if getobjectapi(hdest, len(dinfo), dinfo) = 0 then exit sub if getobjectapi(hbsource, len(bsinfo), bsinfo) = 0 then exit sub if getobjectapi(hfsource, len(fsinfo), fsinfo) = 0 then exit sub if getobjectapi(hmask, len(minfo), minfo) = 0 then exit sub '确定循环次数及偏移 offsetx = iif(dx > 0, dx, 0) offsety = iif(dy > 0, dy, 0) if offsetx >= dinfo.bmwidth or offsety >= dinfo.bmheight then exit sub if bsx > bsinfo.bmwidth or bsy > bsinfo.bmheight or fsx > fsinfo.bmwidth or fsy > fsinfo.bmheight or mx > minfo.bmwidth or my > minfo.bmheight then exit sub '保证范围有效 w = iif(dx >= 0, dw, dw + dx) w = iif(offsetx + w > dinfo.bmwidth, dinfo.bmwidth - offsetx, w) w = iif(bsinfo.bmwidth - bsx > w, w, bsinfo.bmwidth - bsx) w = iif(fsinfo.bmwidth - fsx > w, w, fsinfo.bmwidth - fsx) w = iif(minfo.bmwidth - mx > w, w, minfo.bmwidth - mx) h = iif(dy >= 0, dh, dh + dy) h = iif(offsety + h > dinfo.bmheight, dinfo.bmheight - offsety, h) h = iif(bsinfo.bmheight - bsy > h, h, bsinfo.bmheight - bsy) h = iif(fsinfo.bmheight - fsy > h, h, fsinfo.bmheight - fsy) h = iif(minfo.bmheight - my > h, h, minfo.bmheight - my) if w <= 0 or h <= 0 then exit sub '保证范围有效 '如果为24位位图 if dinfo.bmbitspixel = 24 and bsinfo.bmbitspixel = 24 and fsinfo.bmbitspixel = 24 and minfo.bmbitspixel = 24 then '得到各位图数据 redim ddata(0) as mycolor24, bsdata(0) as mycolor24, fsdata(0) as mycolor24, mdata(0) as mycolor24 redim ddata(dinfo.bmwidth - 1, dinfo.bmheight - 1) as mycolor24 getbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 3, ddata(0, 0) redim bsdata(bsinfo.bmwidth - 1, bsinfo.bmheight - 1) as mycolor24 getbitmapbits hbsource, bsinfo.bmwidth * bsinfo.bmheight * 3, bsdata(0, 0) redim fsdata(fsinfo.bmwidth - 1, fsinfo.bmheight - 1) as mycolor24 getbitmapbits hfsource, fsinfo.bmwidth * fsinfo.bmheight * 3, fsdata(0, 0) redim mdata(minfo.bmwidth - 1, minfo.bmheight - 1) as mycolor24 getbitmapbits hmask, minfo.bmwidth * minfo.bmheight * 3, mdata(0, 0) '处理数据 for y = 0 to h - 1 for x = 0 to w - 1 rate =clng(mdata(x + mx, y + my).r) + mdata(x + mx, y + my).g + mdata(x + mx, y + my).b) / 765 '\3/255 灰度化掩模 ddata(x + dx, y + dy).r = fsdata(x + fsx, y + fsy).r +clng(bsdata(x + bsx, y + bsy).r) - fsdata(x + fsx, y + fsy).r) * rate 'alpha混合 ddata(x + dx, y + dy).g = fsdata(x + fsx, y + fsy).g +clng(bsdata(x + bsx, y + bsy).g) - fsdata(x + fsx, y + fsy).g) * rate 'alpha混合 ddata(x + dx, y + dy).b = fsdata(x + fsx, y + fsy).b +clng(bsdata(x + bsx, y + bsy).b) - fsdata(x + fsx, y + fsy).b) * rate 'alpha混合 next x next y '返回数据 setbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 3, ddata(0, 0) erase ddata, bsdata, fsdata, mdata '如果为32位位图 elseif dinfo.bmbitspixel = 32 and bsinfo.bmbitspixel = 32 and fsinfo.bmbitspixel = 32 and minfo.bmbitspixel = 32 then '得到各位图数据 redim ddata32(0) as mycolor32, bsdata32(0) as mycolor32, fsdata32(0) as mycolor32, mdata32(0) as mycolor32 redim ddata32(dinfo.bmwidth - 1, dinfo.bmheight - 1) as mycolor32 getbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 4, ddata32(0, 0) redim bsdata32(bsinfo.bmwidth - 1, bsinfo.bmheight - 1) as mycolor32 getbitmapbits hbsource, bsinfo.bmwidth * bsinfo.bmheight * 4, bsdata32(0, 0) redim fsdata32(fsinfo.bmwidth - 1, fsinfo.bmheight - 1) as mycolor32 getbitmapbits hfsource, fsinfo.bmwidth * fsinfo.bmheight * 4, fsdata32(0, 0) redim mdata32(minfo.bmwidth - 1, minfo.bmheight - 1) as mycolor32 getbitmapbits hmask, minfo.bmwidth * minfo.bmheight * 4, mdata32(0, 0) '处理数据 for y = 0 to h - 1 for x = 0 to w - 1 rate = ((255 - clng(mdata32(x + mx, y + my).r)) +255 - clng(mdata32(x + mx, y + my).g)) +255 - clng(mdata32(x + mx, y + my).b))) / 765 '\3/255 灰度化掩模 ddata32(x + dx, y + dy).r = fsdata32(x + fsx, y + fsy).r +clng(bsdata32(x + bsx, y + bsy).r) - fsdata32(x + fsx, y + fsy).r) * rate 'alpha混合 ddata32(x + dx, y + dy).g = fsdata32(x + fsx, y + fsy).g +clng(bsdata32(x + bsx, y + bsy).g) - fsdata32(x + fsx, y + fsy).g) * rate 'alpha混合 ddata32(x + dx, y + dy).b = fsdata32(x + fsx, y + fsy).b +clng(bsdata32(x + bsx, y + bsy).b) - fsdata32(x + fsx, y + fsy).b) * rate 'alpha混合 next x next y '返回数据 setbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 4, ddata32(0, 0) erase ddata32, bsdata32, fsdata32, mdata32 end if end sub
发表于:2008-01-23 15:18:1210楼 得分:0
上面的代码有些问题,更新如下:
vbscript code
'------------------ structure ------------------------------------------------ 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 type mycolor24 b as byte g as byte r as byte end type type mycolor32 b as byte g as byte r as byte a as byte end type '------------------ api ------------------------------------------------------ declare function getbitmapbits lib "gdi32" (byval hbitmap as long, byval dwcount as long, lpbits as any) as long declare function setbitmapbits lib "gdi32" (byval hbitmap as long, byval dwcount as long, lpbits as any) as long declare function getobjectapi& lib "gdi32" alias "getobjecta" (byval hobject as long, byval ncount as long, lpobject as any) '----------------- global function ------------------------------------------- '功能:将前景位图经掩模运算后与背景混合,然后按整体透明度复制到目的位图。 '引用:(type)bitmap,(type)mycolor24,(api)getbitmapbits,(api)setbitmapbits,(api)getobjectapi '参数: ' hdestbmp 目的位图句柄 ' dx 目的x坐标 ' dy 目的y坐标 ' dw 目的宽度 ' dh 目的高度 ' hbsourcebmp 背景位图句柄 ' bx 背景x坐标 ' by 背景y坐标 ' hfsourcebmp 前景位图句柄 ' fx 前景x坐标 ' fy 前景y坐标 ' hmaskbmp 掩模位图句柄(可选) ' mx 掩模x坐标(可选) ' my 掩模y坐标(可选) ' transparency 整体透明度(可选)
发表于:2008-01-23 15:19:2311楼 得分:0
vbscript code
sub abimage(byval hdest as long, byval dx as long, byval dy as long, byval dw as long, byval dh as long, byval hbsource as long, byval bx as long, byval by as long, byval hfsource as long, byval fx as long, byval fy as long, optional byval hmask as long = 0, optional byval mx as long = 0, optional byval my as long = 0, optional byval transparency as byte = 0) '各位图的信息 dim dinfo as bitmap, bsinfo as bitmap, fsinfo as bitmap, minfo as bitmap '各位图的数据 dim ddata() as mycolor24, bsdata() as mycolor24, fsdata() as mycolor24, mdata() as mycolor24 dim ddata32() as mycolor32, bsdata32() as mycolor32, fsdata32() as mycolor32, mdata32() as mycolor32 'x,y=循环变量;w,h=循环次数;offsetx,offsety=相对于目的位图坐标偏移 dim x as long, y as long, w as long, h as long, offsetx as long, offsety as long '掩模混合比率, 整体混合比率, 临时数据 dim maskrate as single, alpharate as single, temp as byte alpharate = csng(transparency) / 255 '得到各位图的信息 if getobjectapi(hdest, len(dinfo), dinfo) = 0 then exit sub if getobjectapi(hbsource, len(bsinfo), bsinfo) = 0 then exit sub if getobjectapi(hfsource, len(fsinfo), fsinfo) = 0 then exit sub if getobjectapi(hmask, len(minfo), minfo) = 0 then hmask = 0 '确定循环次数及偏移 offsetx = iif(dx > 0, dx, 0) offsety = iif(dy > 0, dy, 0) if offsetx >= dinfo.bmwidth or offsety >= dinfo.bmheight then exit sub if bx > bsinfo.bmwidth or by > bsinfo.bmheight or fx > fsinfo.bmwidth or fy > fsinfo.bmheight or mx > minfo.bmwidth or my > minfo.bmheight then exit sub '保证范围有效 w = iif(dx >= 0, dw, dw + dx) w = iif(offsetx + w > dinfo.bmwidth, dinfo.bmwidth - offsetx, w) w = iif(bsinfo.bmwidth - bx > w, w, bsinfo.bmwidth - bx) w = iif(fsinfo.bmwidth - fx > w, w, fsinfo.bmwidth - fx) if hmask > 0 then w = iif(minfo.bmwidth - mx > w, w, minfo.bmwidth - mx) h = iif(dy >= 0, dh, dh + dy) h = iif(offsety + h > dinfo.bmheight, dinfo.bmheight - offsety, h) h = iif(bsinfo.bmheight - by > h, h, bsinfo.bmheight - by) h = iif(fsinfo.bmheight - fy > h, h, fsinfo.bmheight - fy) if hmask > 0 then h = iif(minfo.bmheight - my > h, h, minfo.bmheight - my) if w <= 0 or h <= 0 then exit sub '保证范围有效 '如果为24位位图 if dinfo.bmbitspixel = 24 and bsinfo.bmbitspixel = 24 and fsinfo.bmbitspixel = 24 and iif(hmask <> 0, minfo.bmbitspixel = 24, true) then '得到各位图数据 redim ddata(0) as mycolor24, bsdata(0) as mycolor24, fsdata(0) as mycolor24, mdata(0) as mycolor24 redim ddata(dinfo.bmwidth - 1, dinfo.bmheight - 1) as mycolor24 getbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 3, ddata(0, 0) redim bsdata(bsinfo.bmwidth - 1, bsinfo.bmheight - 1) as mycolor24 getbitmapbits hbsource, bsinfo.bmwidth * bsinfo.bmheight * 3, bsdata(0, 0) redim fsdata(fsinfo.bmwidth - 1, fsinfo.bmheight - 1) as mycolor24 getbitmapbits hfsource, fsinfo.bmwidth * fsinfo.bmheight * 3, fsdata(0, 0) if hmask <> 0 then redim mdata(minfo.bmwidth - 1, minfo.bmheight - 1) as mycolor24 getbitmapbits hmask, minfo.bmwidth * minfo.bmheight * 3, mdata(0, 0) end if '处理数据 for y = 0 to h - 1 for x = 0 to w - 1 if hmask <> 0 then maskrate = csng(mdata(x + mx, y + my).r + mdata(x + mx, y + my).g + mdata(x + mx, y + my).b) / 765 '\3/255 灰度化掩模 else maskrate = 0 end if '计算目标红 temp = fsdata(x + fx, y + fy).r +clng(bsdata(x + bx, y + by).r) - fsdata(x + fx, y + fy).r) * maskrate ddata(x + dx, y + dy).r = temp +clng(bsdata(x + bx, y + by).r) - temp) * alpharate '计算目标绿 temp = fsdata(x + fx, y + fy).g +clng(bsdata(x + bx, y + by).g) - fsdata(x + fx, y + fy).g) * maskrate ddata(x + dx, y + dy).g = temp +clng(bsdata(x + bx, y + by).g) - temp) * alpharate '计算目标蓝 temp = fsdata(x + fx, y + fy).b +clng(bsdata(x + bx, y + by).b) - fsdata(x + fx, y + fy).b) * maskrate ddata(x + dx, y + dy).b = temp +clng(bsdata(x + bx, y + by).b) - temp) * alpharate next x next y '返回数据 setbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 3, ddata(0, 0) erase ddata, bsdata, fsdata, mdata '如果为32位位图 elseif dinfo.bmbitspixel = 32 and bsinfo.bmbitspixel = 32 and fsinfo.bmbitspixel = 32 and iif(hmask <> 0, minfo.bmbitspixel = 32, true) then '得到各位图数据 redim ddata32(0) as mycolor32, bsdata32(0) as mycolor32, fsdata32(0) as mycolor32, mdata32(0) as mycolor32 redim ddata32(dinfo.bmwidth - 1, dinfo.bmheight - 1) as mycolor32 getbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 4, ddata32(0, 0) redim bsdata32(bsinfo.bmwidth - 1, bsinfo.bmheight - 1) as mycolor32 getbitmapbits hbsource, bsinfo.bmwidth * bsinfo.bmheight * 4, bsdata32(0, 0) redim fsdata32(fsinfo.bmwidth - 1, fsinfo.bmheight - 1) as mycolor32 getbitmapbits hfsource, fsinfo.bmwidth * fsinfo.bmheight * 4, fsdata32(0, 0) if hmask <> 0 then redim mdata32(minfo.bmwidth - 1, minfo.bmheight - 1) as mycolor32 getbitmapbits hmask, minfo.bmwidth * minfo.bmheight * 4, mdata32(0, 0) end if '处理数据 for y = 0 to h - 1 for x = 0 to w - 1 '灰度化掩模 if hmask <> 0 then maskrate = csng((255 - mdata32(x + mx, y + my).r) +255 - mdata32(x + mx, y + my).g) +255 - mdata32(x + mx, y + my).b)) / 765 '\3/255 else maskrate = 0 end if '计算目标红 temp = fsdata32(x + fx, y + fy).r +clng(bsdata32(x + bx, y + by).r) - fsdata32(x + fx, y + fy).r) * maskrate ddata32(x + dx, y + dy).r = temp +clng(bsdata32(x + bx, y + by).r) - temp) * alpharate '计算目标绿 temp = fsdata32(x + fx, y + fy).g +clng(bsdata32(x + bx, y + by).g) - fsdata32(x + fx, y + fy).g) * maskrate ddata32(x + dx, y + dy).g = temp +clng(bsdata32(x + bx, y + by).g) - temp) * alpharate '计算目标蓝 temp = fsdata32(x + fx, y + fy).b +clng(bsdata32(x + bx, y + by).b) - fsdata32(x + fx, y + fy).b) * maskrate ddata32(x + dx, y + dy).b = temp +clng(bsdata32(x + bx, y + by).b) - temp) * alpharate next x next y '返回数据 setbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 4, ddata32(0