三度网教程:是一个免费提供流行视频软件教程、在线学习分享的学习平台!

制作图像的Mask图

时间:2022-6-10作者:未知来源:三度网教程人气:


制作Mask图形的目的是为了把某一个图,去除其背景颜色,而将之画在另一个图上面,比如说有一张方形的BitMap图,上有一枝笔,而其背景是蓝色,如何将这笔画在另一张图上,而且笔原先的蓝背景不会画上去。
  这个问题便得制造出笔的Mask图,与Invert的Mask图,该Mask图长像是:背景颜色(本例是蓝色)变成白色,其他颜色变成黑色,而Inver的Mask图则反之。

  本范例使用GetMaskPic()来取得Mask图的Handle of Memory DC (hMaskDC),用GetInvertMaskPic()来取得Invert Mask图的Handle of Memory DC (hInvertMaskDC),事实上,如果想要二者同时取得实在没有必要分成两个函式来做,只要一个就好了,这里只是一个范例,告诉大家如果想要个别取得时该如何做啦。

我将这原理简单的说明一下:

1.hdcMono = CreateCompatibleDC(0)
  产生一个Memory DC(与目前萤幕hDC相容),此时该MemoryDC绘的大小只有一个单色pixel

2.hbmpMono = CreateCompatibleBitmap(hdcMono, dx, dy)
  产生一个大小是dx*dy 的Bitmap,而它的第一个叁数是Memory DC所以是单色的BitMap

3.hbmpOld = SelectObject(hdcMono, hbmpMono)
  这个动作一做,该hdcMono的绘图区就变成hbmpMono了,也就是说可以想像hdcMono有一个大小是dx*dy(by pixels)的绘图区,在其上的画图会反映到hBmpMono之上

4.picBack.AutoRedraw = True
  设定picBack.hDc指的是memory中的那份背景图

5.picBack.BackColor = RGB(255, 255, 255)
  设picBack的背景是白色,如此也会同时也使Window background Color变成白色,这边要提出说明的是,vb的BackColor指的是什麽大家都知道,但Window中的BackGround Color却不太相同,Window的BackGround Color指的是在其上画线条或Print文字时,填充於线条或文字外面的颜色,vb中一律使用Form/pictureBox的BackColor来填充,也就是说当BackColor变了时,同时也是让Window中所谓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)
   设定PicBack的Window背景颜色成TColor,同时传回原本的Window BackGround Color存於ColorBack(先前第5步的设定,会使这ColorBack存的是白色的值)

7.BitBlt hdcMono, 0, 0, dx, dy, picBack.hDC, 0, 0, vbSrcCopy
  将picBack的背景图Copy 到memory DC之中,这边有一个重点,picBack是彩色图,但是目前记忆体Dc存的是单色的图,这麽Copy下去的结果会变成:PicBack的BackGround Color(即Tcolor)会变成1(白色),其他颜色变成0(黑色)。注:如果memory dc的图是彩色,就不会如此,而memory dc的图如何变彩色,那便是Step 3. SelectObject时传入一个彩色的hBitmap,而彩色的hBitmap又如何做到,那便是在Step 2.时CreateCompatibleBitmap的第一个叁数,不传Memory DC,而传一个彩色的hDc,如form1.hdc (if 是彩色萤幕)

8.Call SetBkColor(picBack.hDC, ColorBack)
  window BackGround再设回原本的ColorBack之颜色(在这里是白色)

9.BitBlt picBack.hDC, 0, 0, dx, dy, hdcMono, 0, 0, vbSrcCopy
  将Memory dc内的图再画回PicBack的背景图中,相同的,单色图如何放入彩色图呢?原则似乎是(这是我的testing,Win32 help中没有说明): 1(白色)会以目的hdc(在此是picack.hdc)之window Background color来填(此时是ColorBack = 白色) ,而 0(黑色)就以黑色画上去,如此一来,一个图的Mask不就造出来了吗?

10.将Mask的图放到另一个彩色的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上的绘图区设回原先的区域(於Step 2中记录),不过我个人没有这个习惯,但没有做有没有影响系统资源,我是半路出家,实在不知。

12.Call DeleteDC(hdcMono)
  删除记忆体 DC,这没做就不好了。

13.Call DeleteObject(hbmpMono)
  删除Bitmap,这没做就不好了。

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

Call GetInvertMaskPic(Picture1, Image1, RGB(0, 0, 255))
'请确认相对pen.bmp图的背景颜色是什麽,本例中是蓝色,故使用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 '要事先设定
Picture1.BorderStyle = 0
Set Image1.Picture = LoadPicture("c:\pen.bmp") '请自行设定您的图
Set Picture2.Picture = LoadPicture("c:\bkpic.bmp") '请设定成自己的背景图
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 的自订函数,该hMaskDC内的图像是souImg图之背景为白色
'             而souImg的前景图是黑色
'PicBack 叁数: 用来制作 Mask 图的图片盒
'souImg 叁数: 摆放原图的影像之物件,可以是 image/picturebox
'TColor 叁数: 欲去除的颜色,即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)
    '设定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 的自订函数,该hMaskDC内的图像是souImg图之背景为白色
'             而souImg的前景图是黑色
'PicBack 叁数: 用来制作 Mask 图的图片盒
'souImg 叁数: 摆放原图的影像之物件,可以是 image/picturebox
'TColor 叁数: 欲去除的颜色,即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)
    '设定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

关键词:  制作图像的Mask图





Copyright © 2012-2018 三度网教程(http://www.3du8.cn) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版