Code: Select all
ProcedureDLL GrabRegion(ImageID, transcolor) ; HBITMAP ImageID, COLORREF transcolor
;=======================================================
; =
; Very fast bitmap -> region creator =
; =
; By netmaestro =
; =
; Contributors: eesau, nico, flype =
; =
; June 26, 2007 =
; =
;=======================================================
Structure RECTARRAY
rect.RECT[0]
EndStructure
GetObject_(ImageID, SizeOf(BITMAP), @bmp.BITMAP)
Protected width = bmp\bmWidth
Protected height = bmp\bmHeight
Protected hVisibleRgn = CreateRectRgn_(0, 0, width, height)
BmiInfo.BITMAPINFOHEADER
With BmiInfo
\biSize = SizeOf(BITMAPINFOHEADER)
\biWidth = width
\biHeight = -height
\biPlanes = 1
\biBitCount = 32
\biCompression = #BI_RGB
EndWith
rowbytes = SizeOf(LONG)*width
*ColorBits = AllocateMemory(rowbytes*height)
hDC = GetWindowDC_(#Null)
iRes = GetDIBits_(hDC, ImageID, 0, height, *ColorBits, @bmiInfo, #DIB_RGB_COLORS)
ReleaseDC_(#Null, hDC)
Structure_Max=(width*height*16)+SizeOf(RGNDATAHEADER)
*Buffer.RGNDATAHEADER=AllocateMemory(Structure_Max)
*rd.RECTARRAY=*Buffer+SizeOf(RGNDATAHEADER)
rectcount = 0
For y=0 To height-1
pxcount=0
For x=0 To rowbytes-1 Step 4
*px.LONG = *ColorBits + rowbytes * y + x
If *px\l = transcolor
transcount = 1 : firsttrans = pxcount
x+SizeOf(LONG) : *px.LONG = *ColorBits + rowbytes * y + x
While *px\l = transcolor And x <= rowbytes-1
transcount+1 : pxcount+1 : x+SizeOf(LONG)
*px = *ColorBits + rowbytes * y + x
Wend
x-SizeOf(LONG) : *px.LONG = *ColorBits + rowbytes * y + x
*rd\rect[rectcount]\left = firsttrans
*rd\rect[rectcount]\top = y
*rd\rect[rectcount]\right = firsttrans+transcount
*rd\rect[rectcount]\bottom = y+1
rectcount+1
EndIf
pxcount+1
Next
Next
With *Buffer
\dwSize = SizeOf(RGNDATAHEADER)
\iType = #RDH_RECTANGLES
\nCount = rectcount
\nRgnSize = rectcount * SizeOf(RECT)
\rcBound\left = 0
\rcBound\top = 0
\rcBound\right = width
\rcBound\bottom = height
EndWith
RegionSize=SizeOf(RGNDATAHEADER)+(rectcount * SizeOf(RECT))
hTransparentRgn = ExtCreateRegion_(0, RegionSize, *Buffer)
CombineRgn_(hVisibleRgn, hVisibleRgn, hTransparentRgn, #RGN_XOR)
FreeMemory(*Buffer)
FreeMemory(*ColorBits)
DeleteObject_(hTransparentRgn)
ProcedureReturn hVisibleRgn
EndProcedure