国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

如何用VB6創建透明圖象

2024-07-21 02:20:31
字體:
來源:轉載
供稿:網友


透過前面的圖象看到背景圖象,稱前面的圖象為透明圖象,我們見過很多程序和電視節目中都有使用透明圖象,而且大家一定會為圖象的透明而稱奇。究竟透明圖象是如何做出來的呢?下面我們將來探討這種透明圖象的制作方法。 
創建透明圖象的五個必須的步驟: 

準備兩個位圖文件,一個作背景,一個是將要成為透明圖象的源位圖。 

1、 取得源位圖的長、寬數據,依此數據保存一塊和源位圖一樣大小的背景位圖,源位圖將要在這塊背景位圖上繪制。通過用白色像素顯示位圖的透明區域,黑色像素顯示位圖的不透明區域,創建決定位圖透明的單色掩碼。 

2、單色掩碼像素與所用的背景位圖進行二進制“與”(and)位操作,不透明的區域,背景顯示黑色。 

3、用第一步所做的單色掩碼建立一個反向拷貝,再用這個反向拷貝與所用的源位圖進行二進制“與”(and)位操作,源位圖透明的區域將顯示黑色 

4、用第二步修改過的背景和第三步修改的源位圖進行二進制“異或”(xor)位操作,這時可以透過透明位圖看到背景。 

5、把結果位圖復制給背景 

應用實例: 

創建包含一個 commandbutton 控件和兩個picturebox控件的 窗體form1。創建一個模塊(在 "工程”菜單中單擊“添加模塊”)。 

給窗體增加下列控件,設置相關的屬性值: 

控件 name property settings
-----------------------------------------------------------------
picturebox pictsource picture ="c:/flower_vine.bmp"
picturebox pictdest picture ="c:/stones_blue.bmp"
command button command1 caption ="透明圖象"

---- 將下面的代碼粘貼到窗體的聲明部分中, 

---- option explicit ' 這段代碼調用過程transparent()復制源位圖到目標(背景)picturebox控件, ' 并將其變成透明,使人們可以看到后面的背景圖象。 

sub command1_click()
call transparent(pictsource.picture.handle, pictdest, 
10, 10, qbcolor(15))
end sub

---- 將下面的代碼粘貼到模塊的聲明部分中, 

option explicit

---- ' 由于要讀取位圖的基本信息,所以首先要定義一個bitmap結構的變量,然后 

---- ' 利用這一變量來接受位圖的基本信息。 


type bitmap
type as long ' 位圖類型
width as long '寬度
height as long '高度
widthbytes as long '多少二進制位構成一個存儲單位
planes as integer '調色板數
bitspixel as integer '每一個pixel所占用的二進制位數
bits as long '二進制位數據的起始位置 
end type

'api 函數說明
declare function getobject lib "gdi32"
alias "getobjecta" (byval hobject as _
long, byval ncount as long, lpobject as any) as long 
'經由對象的handle取得對象數據結構的api函數

declare function createcompatibledc lib "gdi32" 
(byval hdc as long) as long '
此函數將圖象繪制到存儲器中可避免直 
'接將圖象繪制到屏幕上而造成圖象閃爍
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 '建立位圖對象
declare function createcompatiblebitmap lib
"gdi32" (byval hdc as long, _
byval nwidth as long, byval nheight as long) 
as long '建立兼容性的位圖
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 hsourcedc as long,
byval xsrc as long, _
byval ysrc as long, byval dwrop as long) as long '圖象轉移
declare function setbkcolor lib "gdi32" (byval hdc as long, 
byval crcolor as long) as long
declare function deletedc lib "gdi32" (byval hdc as long)
as long '刪除存儲器dc
declare function selectobject lib "gdi32" (byval hdc as long, 
byval hobject as long) as long '為dc選用對象
declare function deleteobject lib "gdi32" (byval hobject as
long) as long '刪除位圖對象

---- 過程transparent() 復制源位圖到背景的任意 x,y 位置,使這一區域變成透明。transparent()接受五個參數:一個將要變成透明的源位圖,一個目標 picturebox控件 (pictdest), 一個rgb顏色值,另兩個是你想放置原位圖的目的地坐標(destx 和 desty,以像素為單位)。 

sub transparent(byval sourcebmp as long, dest as control, byval _
destx as integer, byval desty as integer, byval transcolor as long)
const pixel = 3
dim sourcedc as long '源位圖
dim destscale as long
dim maskdc as long 'mask位圖 (monochrome)
dim savedc as long '源位圖的備份
dim resultdc as long '源位圖與背景的合并
dim invdc as long 'mask位圖的反向圖
dim origcolor as long '背景色
dim success as long '調用 windows api的結果

dim bmp as bitmap '原位圖的數據結構說明 
dim hresultbmp as long '源與背景的位圖合并
dim hsavebmp as long '原位圖的拷貝
dim hsrcprevbmp as long
dim hdestprevbmp as long
dim hinvbmp as long '反轉掩碼位圖 (monochrome)
dim hprevbmp as long
dim hinvprevbmp as long
dim hsaveprevbmp as long
dim hmaskbmp as long
dim hmaskprevbmp as long


destscale = dest.scalemode '保存 scalemode以便后面恢復
dest.scalemode = pixel '設置 scalemode


sourcedc = createcompatibledc(dest.hdc) '建立存儲器dc
savedc = createcompatibledc(dest.hdc) '建立存儲器dc

invdc = createcompatibledc(dest.hdc) '建立存儲器dc
maskdc = createcompatibledc(dest.hdc) '建立存儲器dc
resultdc = createcompatibledc(dest.hdc) '建立存儲器dc
'接受源位圖得到它的的寬度和長度 (bmp.width , bmp.height)
success = getobject(sourcebmp, len(bmp), bmp)
'創建單色掩碼位圖
hmaskbmp = createbitmap(bmp.width, bmp.height, 1, 1, byval 0&)
hinvbmp = createbitmap(bmp.width, bmp.height, 1, 1, byval 0&)

hresultbmp = createcompatiblebitmap(dest.hdc, bmp.width, _
bmp.height)
hsavebmp = createcompatiblebitmap(dest.hdc, bmp.width, _
bmp.height)
hsrcprevbmp = selectobject(sourcedc, sourcebmp)
hsaveprevbmp = selectobject(savedc, hsavebmp)
hmaskprevbmp = selectobject(maskdc, hmaskbmp)
hinvprevbmp = selectobject(invdc, hinvbmp)
hdestprevbmp = selectobject(resultdc, hresultbmp) '選擇位圖
success = bitblt(savedc, 0, 0, bmp.width, bmp.height, sourcedc, _
0, 0, vbsrccopy) '制作源位圖的拷貝以便后面恢復

origcolor = setbkcolor(sourcedc, transcolor)
success = bitblt(maskdc, 0, 0, bmp.width, bmp.height, sourcedc, _
0, 0, vbsrccopy)
transcolor = setbkcolor(sourcedc, origcolor)

success = bitblt(invdc, 0, 0, bmp.width, bmp.height, maskdc, _
0, 0, vbnotsrccopy)
'拷貝背景圖并創建最終的透明位圖
success = bitblt(resultdc, 0, 0, bmp.width, bmp.height, _
dest.hdc, destx, desty, vbsrccopy)

success = bitblt(resultdc, 0, 0, bmp.width, bmp.height, _
maskdc, 0, 0, vbsrcand)
success = bitblt(sourcedc, 0, 0, bmp.width, bmp.height, invdc, _
0, 0, vbsrcand)

success = bitblt(resultdc, 0, 0, bmp.width, bmp.height, _
sourcedc, 0, 0, vbsrcinvert)

success = bitblt(dest.hdc, destx, desty, bmp.width, bmp.height, _
resultdc, 0, 0, vbsrccopy) '在背景上顯示透明位圖

success = bitblt(sourcedc, 0, 0, bmp.width, bmp.height, savedc, _
0, 0, vbsrccopy) '恢復位圖
'選擇對象以便釋放
hprevbmp = selectobject(resultdc, hdestprevbmp)
hprevbmp = selectobject(sourcedc, hsrcprevbmp)
hprevbmp = selectobject(savedc, hsaveprevbmp)
hprevbmp = selectobject(invdc, hinvprevbmp)
hprevbmp = selectobject(maskdc, hmaskprevbmp)
'釋放資源
success = deletedc(savedc)
success = deletedc(invdc)
success = deletedc(resultdc)
success = deleteobject(hsavebmp)
success = deleteobject(hmaskbmp)
success = deleteobject(hinvbmp)
success = deletedc(sourcedc)
success = deletedc(maskdc)

success = deleteobject(hresultbmp)
dest.scalemode = destscale '恢復 scalemode
end sub

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 徐汇区| 寻乌县| 太原市| 高台县| 怀集县| 宜都市| 科尔| 报价| 禹州市| 彰武县| 遂川县| 永靖县| 乌拉特后旗| 玉树县| 加查县| 巨野县| 保亭| 黎平县| 新竹市| 肥乡县| 万荣县| 元朗区| 龙川县| 手游| 沙田区| 乐陵市| 酉阳| 来安县| 栾城县| 十堰市| 丹巴县| 西乌| 潼南县| 丹寨县| 彩票| 喜德县| 临洮县| 班玛县| 突泉县| 新化县| 桑日县|