#Image from ImageID

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: #Image from ImageID

Post by mk-soft »

also available in "Window Manager" include

http://www.purebasic.fr/english/viewtop ... 12&t=59124

Code: Select all

;- *** Image Objects ***

Structure ImageData
  handle.i
  id.i
  filename.s
EndStructure

Global NewMap ListImages.ImageData()

; ---------------------------------------------------------------------------------------

Procedure MyLoadImage(Image, Filename.s)
  
  Protected result, handle, id, key.s
  
  result = LoadImage(Image, Filename)
  If result = 0
    ProcedureReturn 0
  EndIf
  If Image = #PB_Any
    handle = ImageID(result)
    id = result
  Else
    handle = result
    id = Image
  EndIf
  
  key = Str(handle)
  AddMapElement(ListImages(), key)
  With ListImages()
    \handle = handle
    \id = id
    \filename = Filename
  EndWith
  
  ProcedureReturn result
  
EndProcedure

Macro LoadImage(Image, Filename)
  MyLoadImage(Image, Filename)
EndMacro

; ---------------------------------------------------------------------------------------

Procedure MyCatchImage(Image, *Memory, Size = 0)
  
  Protected result, handle, id, key.s
  
  result = CatchImage(Image, *Memory, Size)
  If result = 0
    ProcedureReturn 0
  EndIf
  If Image = #PB_Any
    handle = ImageID(result)
    id = result
  Else
    handle = result
    id = Image
  EndIf
  key = Str(handle)
  AddMapElement(ListImages(), key)
  With ListImages()
    \handle = handle
    \id = id
    \filename = ":memory:"
  EndWith
  
  ProcedureReturn result
  
EndProcedure

Macro CatchImage(Image, Memory, Size = 0)
  MyCatchImage(Image, Memory, Size)
EndMacro

; ---------------------------------------------------------------------------------------

Procedure MyFreeImage(Image)
  
  Protected key.s
  
  If IsImage(Image)
    key = Str(ImageID(Image))
    DeleteMapElement(ListImages(), key)
    FreeImage(Image)
  EndIf
  
EndProcedure

Macro FreeImage(Image)
  MyFreeImage(Image)
EndMacro

; ---------------------------------------------------------------------------------------

Procedure GetImageID(Handle)
  
  Protected result, key.s
  
  result = -1
  key = Str(Handle)
  If FindMapElement(ListImages(), key)
    result = ListImages()\id
  EndIf
  
  ProcedureReturn result
  
EndProcedure
 
; ---------------------------------------------------------------------------------------
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

Re: #Image from ImageID

Post by Julian »

Thanks Danilo, I'll look into that. Thanks Trond and Mk, very cool idea there with the macro!

Plenty for me to get stuck in to here.

Thanks all
Post Reply