想快速地对指定图片或区域进行滤光处理,常见的方法取出图片数据,逐像素与指定滤光色进行AND运算,很麻烦。还见过网上流传的有用Point或GetPixel取点运算的,其低速可想而知。
其实利用BitBlt的位运算,可高速完成这种操作,下面这个函数比数组运算方法可快10倍,比Point或GetPixel估计会快上千倍。
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32.dll" (ByVal crColor As Long) As Long
Private Declare Function FillRect Lib "user32.dll" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Private 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
Public Sub FilterRGB(dHdc As Long, X As Long, y As Long, w As Long, h As Long, Optional tc As Long = &HFFFF00)
Dim tmphdc As MemHdc, rc As RECT, hBrush As Long
tmphdc = NewMyHdc(dHdc, w, h) '建立一个内存位图
rc.Right = w
rc.Bottom = h
hBrush = CreateSolidBrush(tc)
FillRect tmphdc.hdc, rc, hBrush '用滤光色填充图片,产生一个纯色图片
DeleteObject hBrush
BitBlt dHdc, X, y, w, h, tmphdc.hdc, 0, 0, vbSrcAnd '绘入目标,并与目标进行And运算,达到滤光效果
tmphdc = DelMyHdc(tmphdc)
End Sub
本篇中的公用函数NewMyHdc、DelMyHdc及相关结构与API声明,可在以下文章中找到