明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

制作圖像的Mask圖

[摘要]制作Mask圖形的目的是為了把某一個(gè)圖,去除其背景顏色,而將之畫在另一個(gè)圖上面,比如說有一張方形的BitMap圖,上有一枝筆,而其背景是藍(lán)色,如何將這筆畫在另一張圖上,而且筆原先的藍(lán)背景不會畫上去。...
制作Mask圖形的目的是為了把某一個(gè)圖,去除其背景顏色,而將之畫在另一個(gè)圖上面,比如說有一張方形的BitMap圖,上有一枝筆,而其背景是藍(lán)色,如何將這筆畫在另一張圖上,而且筆原先的藍(lán)背景不會畫上去。
  這個(gè)問題便得制造出筆的Mask圖,與Invert的Mask圖,該Mask圖長像是:背景顏色(本例是藍(lán)色)變成白色,其他顏色變成黑色,而Inver的Mask圖則反之。

  本范例使用GetMaskPic()來取得Mask圖的Handle of Memory DC (hMaskDC),用GetInvertMaskPic()來取得Invert Mask圖的Handle of Memory DC (hInvertMaskDC),事實(shí)上,如果想要二者同時(shí)取得實(shí)在沒有必要分成兩個(gè)函式來做,只要一個(gè)就好了,這里只是一個(gè)范例,告訴大家如果想要個(gè)別取得時(shí)該如何做啦。

我將這原理簡單的說明一下:

1.hdcMono = CreateCompatibleDC(0)
  產(chǎn)生一個(gè)Memory DC(與目前螢?zāi)籬DC相容),此時(shí)該MemoryDC繪的大小只有一個(gè)單色pixel

2.hbmpMono = CreateCompatibleBitmap(hdcMono, dx, dy)
  產(chǎn)生一個(gè)大小是dx*dy 的Bitmap,而它的第一個(gè)叁數(shù)是Memory DC所以是單色的BitMap

3.hbmpOld = SelectObject(hdcMono, hbmpMono)
  這個(gè)動作一做,該hdcMono的繪圖區(qū)就變成hbmpMono了,也就是說可以想像hdcMono有一個(gè)大小是dx*dy(by pixels)的繪圖區(qū),在其上的畫圖會反映到hBmpMono之上

4.picBack.AutoRedraw = True
  設(shè)定picBack.hDc指的是memory中的那份背景圖

5.picBack.BackColor = RGB(255, 255, 255)
  設(shè)picBack的背景是白色,如此也會同時(shí)也使Window background Color變成白色,這邊要提出說明的是,vb的BackColor指的是什麼大家都知道,但Window中的BackGround Color卻不太相同,Window的BackGround Color指的是在其上畫線條或Print文字時(shí),填充於線條或文字外面的顏色,vb中一律使用Form/pictureBox的BackColor來填充,也就是說當(dāng)BackColor變了時(shí),同時(shí)也是讓W(xué)indow中所謂Background Color變,可以用以下程式一試便知:
  me.backcolor = RGB(255,255,255)
  Call SetBkColor(me.hDc, RGB(255,0,0))
  me.print "haha"

6.ColorBack = SetBkColor(picBack.hDC, TColor)
   設(shè)定PicBack的Window背景顏色成TColor,同時(shí)傳回原本的Window BackGround Color存於ColorBack(先前第5步的設(shè)定,會使這ColorBack存的是白色的值)

7.BitBlt hdcMono, 0, 0, dx, dy, picBack.hDC, 0, 0, vbSrcCopy
  將picBack的背景圖Copy 到memory DC之中,這邊有一個(gè)重點(diǎn),picBack是彩色圖,但是目前記憶體Dc存的是單色的圖,這麼Copy下去的結(jié)果會變成:PicBack的BackGround Color(即Tcolor)會變成1(白色),其他顏色變成0(黑色)。注:如果memory dc的圖是彩色,就不會如此,而memory dc的圖如何變彩色,那便是Step 3. SelectObject時(shí)傳入一個(gè)彩色的hBitmap,而彩色的hBitmap又如何做到,那便是在Step 2.時(shí)CreateCompatibleBitmap的第一個(gè)叁數(shù),不傳Memory DC,而傳一個(gè)彩色的hDc,如form1.hdc (if 是彩色螢?zāi)?

8.Call SetBkColor(picBack.hDC, ColorBack)
  window BackGround再設(shè)回原本的ColorBack之顏色(在這里是白色)

9.BitBlt picBack.hDC, 0, 0, dx, dy, hdcMono, 0, 0, vbSrcCopy
  將Memory dc內(nèi)的圖再畫回PicBack的背景圖中,相同的,單色圖如何放入彩色圖呢?原則似乎是(這是我的testing,Win32 help中沒有說明): 1(白色)會以目的hdc(在此是picack.hdc)之window Background color來填(此時(shí)是ColorBack = 白色) ,而 0(黑色)就以黑色畫上去,如此一來,一個(gè)圖的Mask不就造出來了嗎?

10.將Mask的圖放到另一個(gè)彩色的Memory DC之中
  hMaskDC = CreateCompatibleDC(0)
  hBmpMask = CreateCompatibleBitmap(picBack.hDc, dx, dy)
  Call SelectObject(hMaskDC, hBmpMask)
  BitBlt hMaskDC, 0, 0, dx, dy, picBack.hDc, 0, 0, vbSrcCopy

11.Call SelectObject(hdcMono, hbmpOld)
  因已大功告成,所以將hdcMono上的繪圖區(qū)設(shè)回原先的區(qū)域(於Step 2中記錄),不過我個(gè)人沒有這個(gè)習(xí)慣,但沒有做有沒有影響系統(tǒng)資源,我是半路出家,實(shí)在不知。

12.Call DeleteDC(hdcMono)
  刪除記憶體 DC,這沒做就不好了。

13.Call DeleteObject(hbmpMono)
  刪除Bitmap,這沒做就不好了。

'以下在form 需二個(gè)PictureBox,一個(gè)Image Control, 一個(gè)Command Box
Private Sub Command1_Click()
Dim dx As Long, dy As Long

Call GetInvertMaskPic(Picture1, Image1, RGB(0, 0, 255))
'請確認(rèn)相對pen.bmp圖的背景顏色是什麼,本例中是藍(lán)色,故使用RGB(0,0,255)
Call GetMaskPic(Picture1, Image1, RGB(0, 0, 255))

dx = Me.ScaleX(Image1.Picture.Width, vbHimetric, vbPixels)
dy = Me.ScaleY(Image1.Picture.Height, vbHimetric, vbPixels)

'以下將image1的圖去除背景畫在Picture2之上
Set Picture1.Picture = Image1.Picture
BitBlt Picture2.hDc, 0, 0, dx, dy, hMaskDC, 0, 0, vbSrcAnd
BitBlt Picture1.hDc, 0, 0, dx, dy, hInvertMaskDC, 0, 0, vbSrcAnd
BitBlt Picture2.hDc, 0, 0, dx, dy, Picture1.hDc, 0, 0, vbSrcPaint

End Sub

Private Sub Form_Load()
Picture1.Visible = False
Picture1.AutoRedraw = True
'Picture1.Appearance = 0 '要事先設(shè)定
Picture1.BorderStyle = 0
Set Image1.Picture = LoadPicture("c:\pen.bmp") '請自行設(shè)定您的圖
Set Picture2.Picture = LoadPicture("c:\bkpic.bmp") '請?jiān)O(shè)定成自己的背景圖
End Sub


Private Sub Form_Unload(Cancel As Integer)
  Call DeleteDC(hMaskDC)
  Call DeleteObject(hBmpMask)
  Call DeleteDC(hInvertMaskDC)
  Call DeleteObject(hBmpInvertMask)

End Sub



'以下在.BAS

Declare Function CreateCompatibleBitmap Lib "GDI32" _
   (ByVal hDc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Declare Function CreateCompatibleDC Lib "GDI32" _
   (ByVal hDc As Long) As Long
Declare Function DeleteObject Lib "GDI32" _
   (ByVal hObject As Long) As Long
Declare Function SelectObject Lib "GDI32" _
   (ByVal hDc As Long, ByVal hObject As Long) As Long
Declare Function DeleteDC Lib "GDI32" _
   (ByVal hDc 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 hSrcDC 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

Public hMaskDC As Long, hBmpMask As Long
Public hInvertMaskDC As Long, hBmpInvertMask As Long

'取得 hMaskDC 的自訂函數(shù),該hMaskDC內(nèi)的圖像是souImg圖之背景為白色
'             而souImg的前景圖是黑色
'PicBack 叁數(shù): 用來制作 Mask 圖的圖片盒
'souImg 叁數(shù): 擺放原圖的影像之物件,可以是 image/picturebox
'TColor 叁數(shù): 欲去除的顏色,即souImg的背景色
Public Sub GetMaskPic(picBack As PictureBox, _
    souImg As Control, ByVal TColor As Long)
Dim hdcMono, hbmpMono, hbmpOld
Dim ColorBack As Long
Dim dx As Long, dy As Long

  With picBack
    '取得該圖的大小, by Pixels
    dx = .ScaleX(souImg.Picture.Width, vbHimetric, vbPixels)
    dy = .ScaleY(souImg.Picture.Height, vbHimetric, vbPixels)
    '設(shè)定pictureBox的大小與Source Image的大小相同
    .Width = souImg.Width
    .Height = souImg.Height
    Set .Picture = souImg.Picture
  End With
  
  hdcMono = CreateCompatibleDC(0)
  hbmpMono = CreateCompatibleBitmap(hdcMono, dx, dy)
  hbmpOld = SelectObject(hdcMono, hbmpMono)
  
  picBack.AutoRedraw = True
  picBack.BackColor = RGB(255, 255, 255)
  
  ColorBack = SetBkColor(picBack.hDc, TColor)
  BitBlt hdcMono, 0, 0, dx, dy, picBack.hDc, 0, 0, vbSrcCopy
  Call SetBkColor(picBack.hDc, ColorBack)
  BitBlt picBack.hDc, 0, 0, dx, dy, hdcMono, 0, 0, vbSrcCopy
  
  hMaskDC = CreateCompatibleDC(0)
  hBmpMask = CreateCompatibleBitmap(picBack.hDc, dx, dy)
  Call SelectObject(hMaskDC, hBmpMask)
  BitBlt hMaskDC, 0, 0, dx, dy, picBack.hDc, 0, 0, vbSrcCopy
 
  Call SelectObject(hdcMono, hbmpOld)
  Call DeleteDC(hdcMono)
  Call DeleteObject(hbmpMono)
  
End Sub

'取得 hInvertMaskDC 的自訂函數(shù),該hMaskDC內(nèi)的圖像是souImg圖之背景為白色
'             而souImg的前景圖是黑色
'PicBack 叁數(shù): 用來制作 Mask 圖的圖片盒
'souImg 叁數(shù): 擺放原圖的影像之物件,可以是 image/picturebox
'TColor 叁數(shù): 欲去除的顏色,即souImg的背景色
Public Sub GetInvertMaskPic(picBack As PictureBox, _
    souImg As Control, ByVal TColor As Long)
Dim hdcMono, hbmpMono, hbmpOld
Dim ColorBack As Long
Dim dx As Single, dy As Single

  With picBack
    dx = .ScaleX(souImg.Picture.Width, vbHimetric, vbPixels)
    dy = .ScaleY(souImg.Picture.Height, vbHimetric, vbPixels)
    '設(shè)定pictureBox的大小與Source Image的大小相同
    .Width = souImg.Width
    .Height = souImg.Height
    Set .Picture = souImg.Picture
  End With
  
  hdcMono = CreateCompatibleDC(0)
  hbmpMono = CreateCompatibleBitmap(hdcMono, dx, dy)
  hbmpOld = SelectObject(hdcMono, hbmpMono)
  
  picBack.AutoRedraw = True
  picBack.BackColor = RGB(255, 255, 255)
  
  ColorBack = SetBkColor(picBack.hDc, TColor)
  BitBlt hdcMono, 0, 0, dx, dy, picBack.hDc, 0, 0, vbSrcCopy
  Call SetBkColor(picBack.hDc, ColorBack)
  BitBlt picBack.hDc, 0, 0, dx, dy, hdcMono, 0, 0, vbNotSrcCopy
    
  hInvertMaskDC = CreateCompatibleDC(0)
  hBmpInvertMask = CreateCompatibleBitmap(picBack.hDc, dx, dy)
  Call SelectObject(hInvertMaskDC, hBmpInvertMask)
  BitBlt hInvertMaskDC, 0, 0, dx, dy, picBack.hDc, 0, 0, vbSrcCopy

  Call SelectObject(hdcMono, hbmpOld)
  Call DeleteDC(hdcMono)
  Call DeleteObject(hbmpMono)
  
End Sub


標(biāo)簽:制作圖像的Mask圖