Which is the best userlib (stability & compatibility wise) floating around for using transparency in images? (I want to be able to use some images in my app, but be able to set a certain rgb color to be transparent.)
There are a couple of dirrerent libs on PureArea that can do this, but I would rather have the opinions of those who have experience with the libs, which is the best to use?
Thanks
transparent image library?
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Both these routines are well tested and should work without problems
under windows:
under windows:
Code: Select all
; For drawing the image "as is":
Procedure DrawTransparentImage(DC, ImageID, x, y, TransparentColor) ;Draw Transparent Image
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
; For drawing and sizing the image:
ProcedureDLL DrawTransparentImageEx(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
; To use these routines you need the device context (DC)
; You can get this easily with DC = StartDrawing(... etc.
BERESHEIT
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Here's an example of basic usage:
Code: Select all
Procedure DrawTransparentImage(DC, ImageID, x, y, TransparentColor) ;Draw Transparent Image
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
OpenWindow(0,0,0,640,480,"Transparent Drawing",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateImage(0,256,256)
StartDrawing(ImageOutput(0))
Box(0,0,256,256,#Green)
Circle(127,127,128,#Red)
Circle(127,127,108,#Green)
StopDrawing()
hDC=StartDrawing(WindowOutput(0))
DrawTransparentImage(hDC, ImageID(0), 200, 100, #Green)
StopDrawing()
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
BERESHEIT