Page 1 of 1

transparent image library?

Posted: Wed Mar 22, 2006 4:39 am
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

Posted: Wed Mar 22, 2006 1:20 pm
by Polo
It's possible without userlibrary, even though it's buggy with the Point/Plot command.

Posted: Wed Mar 22, 2006 5:26 pm
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.

Posted: Thu Mar 23, 2006 6:37 pm
by Brice Manuel
Thank you very much, Now if i can figure out how to properly implement it, I shoudl be set :wink:

Posted: Thu Mar 23, 2006 6:51 pm
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

Posted: Fri Mar 24, 2006 9:03 pm
by Brice Manuel
THanks a million, you have been a huge help :!:

Posted: Fri Mar 24, 2006 10:51 pm
by rsts
Very nice.

Thanks for showing us.

cheers,