transparent image library?

Everything else that doesn't fall into one of the other PB categories.
Brice Manuel

transparent image library?

Post by Brice Manuel »

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
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

It's possible without userlibrary, even though it's buggy with the Point/Plot command.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Both these routines are well tested and should work without problems
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
Brice Manuel

Post by Brice Manuel »

Thank you very much, Now if i can figure out how to properly implement it, I shoudl be set :wink:
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

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
Brice Manuel

Post by Brice Manuel »

THanks a million, you have been a huge help :!:
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Very nice.

Thanks for showing us.

cheers,
Post Reply