There are a couple of tried-and-true methods kicking around the forum for drawing transparent images. They work well, one is reasonably fast and the other one is fairly slow. I wondered if the speed could be improved, and so I made some experiments, and from the results I made a new procedure. It works the same as the others but the speed - wow. I'm getting 68 thousand images rendered per second! Here's my test, see what numbers you get:
Code: Select all
Import "msimg32.lib"
TransparentBlt(a,b,c,d,e,f,g,h,i,j,k)
EndImport
OpenWindow(0,0,0,640,480,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateImage(0, 128,128)
StartDrawing(ImageOutput(0))
Box(0,0,128,128,#Blue)
Box(5,5,118,118,RGB(255,0,255))
StopDrawing()
ResizeImage(0,128,128)
CreateImage(1, 128,128)
StartDrawing(ImageOutput(1))
Box(0,0,128,128,GetSysColor_(#COLOR_BTNFACE))
StopDrawing()
Procedure CommonMethod1(DC, Bitmap, x, y, Width, Height, TransparentColor)
maskDC = CreateCompatibleDC_(DC)
tempDC = CreateCompatibleDC_(DC)
SourceDC = CreateCompatibleDC_(DC)
SelectObject_(SourceDC, Bitmap)
hMaskBmp = CreateBitmap_(Width, Height, 1, 1, 0)
hTempBmp = CreateCompatibleBitmap_(DC, Width, Height)
SelectObject_(maskDC, hMaskBmp)
SelectObject_(tempDC, hTempBmp)
TransparentColor= SetBkColor_(SourceDC, TransparentColor)
BitBlt_ (maskDC, 0, 0, Width, Height, SourceDC, 0, 0, #SRCCOPY)
SetBkColor_(SourceDC, TransparentColor)
BitBlt_ (tempDC, 0, 0, Width, Height, maskDC, 0, 0, #SRCCOPY)
BitBlt_ (DC, X, Y, Width, Height, tempDC, 0, 0, #MERGEPAINT)
BitBlt_ (maskDC, 0, 0, Width, Height, maskDC, 0, 0, #NOTSRCCOPY)
BitBlt_ (tempDC, 0, 0, Width, Height, SourceDC, 0, 0, #SRCCOPY)
BitBlt_ (tempDC, 0, 0, Width, Height, maskDC, 0, 0, #MERGEPAINT)
BitBlt_ (DC, X, Y, Width, Height, tempDC, 0, 0, #SRCAND)
DeleteObject_ (hMaskBmp)
DeleteObject_ (hTempBmp)
DeleteDC_ (maskDC)
DeleteDC_ (tempDC)
DeleteDC_ (SourceDC)
EndProcedure
Procedure CommonMethod2(DC, ImageID, x, y, TransparentColor)
Protected ImageList, BM.BITMAP
GetObject_(ImageID,SizeOf(BITMAP),BM.BITMAP)
ImageID = CopyImage_(ImageID,#IMAGE_BITMAP,BM\bmWidth,BM\bmHeight,0)
ImageList = ImageList_Create_(BM\bmWidth,BM\bmHeight,#ILC_COLORDDB|#ILC_MASK,1,0)
ImageList_AddMasked_(ImageList,ImageID,TransparentColor)
ImageList_Draw_(ImageList,0,DC,x,y,#ILD_TRANSPARENT)
ImageList_Destroy_(ImageList)
DeleteObject_(ImageID)
EndProcedure
Procedure MyNewFavouriteMethod(outputdc, image, x, y, transcolor)
tmpdc=StartDrawing(ImageOutput(image))
TransparentBlt(outputdc, x, y, ImageWidth(image), ImageHeight(image), tmpdc, 0, 0, ImageWidth(image), ImageHeight(image), transcolor)
StopDrawing()
ReleaseDC_(0, outputdc)
EndProcedure
Procedure cls()
StartDrawing(WindowOutput(0))
Box(0,0,640,480,GetSysColor_(#COLOR_BTNFACE))
StopDrawing()
EndProcedure
cls()
Delay(300)
cc.d=0
s1=ElapsedMilliseconds()
While ElapsedMilliseconds()-s1 <= 2000
CommonMethod2(GetDC_(WindowID(0)), ImageID(0), Random(600),Random(400), RGB(255,0,255))
cc+1
Wend
str2.s = "Common Method 2: "+Str(Int(cc/(ElapsedMilliseconds()-s1)*1000))+" images rendered per second"
cls()
Delay(300)
cc.d=0
s1=ElapsedMilliseconds()
While ElapsedMilliseconds()-s1 <= 2000
CommonMethod1(GetDC_(WindowID(0)), ImageID(0), Random(600),Random(400),ImageWidth(0), ImageHeight(0), RGB(255,0,255))
cc+1
Wend
str1.s = "Common Method 1: "+Str(Int(cc/(ElapsedMilliseconds()-s1)*1000))+" images rendered per second"
cls()
Delay(300)
cc.d=0
s1=ElapsedMilliseconds()
While ElapsedMilliseconds()-s1 <= 2000
MyNewFavouriteMethod(GetDC_(WindowID(0)), 0, Random(600),Random(400), RGB(255,0,255))
cc+1
Wend
str0.s = "My New Favourite Method: "+Str(Int(cc/(ElapsedMilliseconds()-s1)*1000))+" images rendered per second!! "
; Debug str2
; Debug str1
; Debug str0
MessageRequester("Results", str2 + #LF$ + str1 + #LF$ + str0)
Repeat:Until WaitWindowEvent()=#WM_CLOSE