Code: Select all
Procedure DrawTransparentImage(DC, Bitmap, x, y, Width, Height, TransparentColor)
; First, create some DC's. These are our gateways To associated
; bitmaps in RAM
maskDC = CreateCompatibleDC_(DC)
tempDC = CreateCompatibleDC_(DC)
SourceDC = CreateCompatibleDC_(DC)
SelectObject_(SourceDC, Bitmap)
; Then, we need the bitmaps. Note that we create a monochrome
; bitmap here!
; This is a trick we use For creating a mask fast enough.
hMaskBmp = CreateBitmap_(Width, Height, 1, 1, 0)
hTempBmp = CreateCompatibleBitmap_(DC, Width, Height)
; Then we can assign the bitmaps to the DCs
;
hMaskBmp2 = SelectObject_(maskDC, hMaskBmp)
hTempBmp2 = SelectObject_(tempDC, hTempBmp)
; Now we can create a mask. First, we set the background color
; To the transparent color; then we copy the image into the
; monochrome bitmap.
; When we are done, we reset the background color of the
; original source.
TransparentColor= SetBkColor_(SourceDC, TransparentColor)
BitBlt_ (maskDC, 0, 0, Width, Height, SourceDC, 0, 0, #SRCCOPY)
SetBkColor_(SourceDC, TransparentColor)
; The first we do with the mask is To MergePaint it into the
; destination.
; This will punch a WHITE hole in the background exactly were
; we want the graphics To be painted in.
BitBlt_ (tempDC, 0, 0, Width, Height, maskDC, 0, 0, #SRCCOPY)
BitBlt_ (DC, X, Y, Width, Height, tempDC, 0, 0, #MERGEPAINT)
; Now we delete the transparent part of our source image. To do
; this, we must invert the mask And MergePaint it into the
; source image. The transparent area will now appear as WHITE.
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)
; Both target And source are clean. All we have To do is To And
; them together!
BitBlt_ (DC, X, Y, Width, Height, tempDC, 0, 0, #SRCAND)
; Now all we have To do is To clean up after us And free system
; resources..
DeleteObject_ (hMaskBmp)
DeleteObject_ (hTempBmp)
DeleteObject_ (hMaskBmp2)
DeleteObject_ (hTempBmp2)
DeleteDC_ (maskDC)
DeleteDC_ (tempDC)
DeleteDC_ (SourceDC)
EndProcedure
Enumeration ;Images
#Image1
#Image2
EndEnumeration
CreateImage(#Image1,32,32)
CreateImage(#Image2,32,32)
UseImage(#Image1)
StartDrawing(ImageOutput())
Box(0,0,32,32,RGB(255,0,0))
Box(5,5,22,22,RGB(0,0,0))
StopDrawing()
UseImage(#Image2)
StartDrawing(ImageOutput())
Box(0,0,32,32,RGB(0,0,255))
Box(5,5,22,22,RGB(0,0,0))
StopDrawing()
OpenWindow(0,0,0,200,200,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"DrawTransparentImage() NO BUG")
Repeat
Event = WindowEvent()
X = Random(168)
Y = Random(168)
Image = Random(#Image2)
DC = StartDrawing(WindowOutput())
DrawTransparentimage(DC,UseImage(Image),X,Y,32,32,RGB(0,0,0))
ReleaseDC_(WindowID(0),DC)
StopDrawing()
Until Event = #PB_Event_CloseWindow
sources like this...sometimes this could be helpful.
ImageList_AddMasked, DIBs, or such things.