摘要:圖形遮罩的應用
背景透空的需求常常會遇到
實作方式也有很多種
API就有直接能做到的方法: TransperantBlt 只要指定透空的顏色就可以直接做出
但這種做法不是用於有些時候, 實際要顯示的圖形裡面也含有透空顏色
會變成圖形貼過去會破掉(這其實應該是美工的問題和責任)
所以需要做這種東西的時候, 我通常會有兩種作法
1. 逼迫美工作出適合透空的圖形來給我用, 這樣就可以直接利用TransperantBlt
2. 逼迫美工給我背景透空的GIF, 這不需要任何處理直接可以用GDI+函數畫到畫面上
這裏使用的就只是.DrawImage方法而已
3. 做遮罩, 方法如下
先準備兩張圖, 分別放在picturebox3和picturebox2裡面, 另外畫面上有一個picturebox1作為繪圖的地方
一張是背景白色, 物體"純黑色"的圖形
一張是背景純黑色, 物體就正確存在的圖形
並且先在程式最上面宣告函數原型
Declare Function
BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As System.Int32) As Boolean
然後就可以按順序執行下列兩段程式來看效果
1 Dim 鴨子 As Graphics = Me.PictureBox3.CreateGraphics
2 Dim dcDest As IntPtr = Me.PictureBox1.CreateGraphics.GetHdc()
3 Dim dcSrc1 As IntPtr = 鴨子.GetHdc()
4 BitBlt(dcDest, 250, 190, 100, 50, dcSrc1, 0, 0, 8913094) < --這個8913094的數字為and運算
2 Dim dcDest As IntPtr = Me.PictureBox1.CreateGraphics.GetHdc()
3 Dim dcSrc1 As IntPtr = 鴨子.GetHdc()
4 BitBlt(dcDest, 250, 190, 100, 50, dcSrc1, 0, 0, 8913094) < --這個8913094的數字為and運算
1 Dim 鴨子 As Graphics = Me.PictureBox2.CreateGraphics
2 Dim dcDest As IntPtr = Me.PictureBox1.CreateGraphics.GetHdc()
3 Dim dcSrc1 As IntPtr = 鴨子.GetHdc()
4 BitBlt(dcDest, 250, 190, 100, 50, dcSrc1, 0, 0, 15597702) <--這個15597702的數字為or運算
2 Dim dcDest As IntPtr = Me.PictureBox1.CreateGraphics.GetHdc()
3 Dim dcSrc1 As IntPtr = 鴨子.GetHdc()
4 BitBlt(dcDest, 250, 190, 100, 50, dcSrc1, 0, 0, 15597702) <--這個15597702的數字為or運算
執行結果
順著天賦做事,逆著個性做人生命, 就該浪費在美好的事物上