Get Image from ImageID

Just starting out? Need help? Post your questions and find answers here.
dougmo52usr
User
User
Posts: 62
Joined: Mon Jul 18, 2016 6:43 pm

Get Image from ImageID

Post by dougmo52usr »

I did a Google search for "#Image from ImageID", and found a link to a forum question. The response to the question on the google link was
Hi Julian. Until the team implements the long-lamented GetImageNumber (fromImageID) function, here's a simple drop-in solution that might work for you. It's comprised of two short procedures, one to initialise all the images in a project,
It seems that many links from Google to the forum are broken. I then did a search on the forum for "#Image from ImageID" and got no results.

So, I want to resize the image on a ButtonImageGadget when it's container is resized. I can call GetGadgetAttribute(EventGadget(),#PB_Button_Image) to get the ImageID, but ResizeImage doesn't take an ImageID, so I need to retrieve the image.

Is there a way to do it?
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get Image from ImageID

Post by infratec »

Store the image number with SetGadgetData()
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Get Image from ImageID

Post by RASHAD »

If the Image was created by PureBasic

https://www.purebasic.fr/english/viewto ... lo#p464623
Egypt my love
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Get Image from ImageID

Post by mk-soft »

Here is something I made. 8) :wink:

You should always keep the original image to create the scaled image from it.
Place the reference to the images in the gadget data.

Sorry, most of the code for the window, the events and update window is a ready-made template for me. 8)

Update
- Min image size

Code: Select all

;-TOP

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainButton1
  #MainButton2
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

; ----

Structure udtButtonImage
  Image.i
  ScaleImage.i
EndStructure

Global ButtonImage1.udtButtonImage
Global ButtonImage2.udtButtonImage

Procedure InitProgram()
  ButtonImage1\Image = LoadImage(#PB_Any, #PB_Compiler_Home + "examples/sources/Data/PureBasic.bmp")
  ButtonImage2\Image = LoadImage(#PB_Any, #PB_Compiler_Home + "examples/sources/Data/Map.bmp")
EndProcedure : InitProgram()

Procedure UpdateImageButton(Gadget)
  Protected dx, dy, *ButtonImage.udtButtonImage
  
  With *ButtonImage
    *ButtonImage = GetGadgetData(Gadget)
    If *ButtonImage
      dx = GadgetWidth(Gadget) - 10
      If dx < 16
        dx = 16
      EndIf
      dy = GadgetHeight(Gadget) - 10
      If dy < 16
        dy = 16
      EndIf
      If \Image
        If \ScaleImage
          FreeImage(\ScaleImage)
        EndIf
        \ScaleImage = CopyImage(\Image, #PB_Any)
        ResizeImage(\ScaleImage, dx, dy)
        SetGadgetAttribute(Gadget, #PB_Button_Image, ImageID(\ScaleImage))
      EndIf
    EndIf
  EndWith
EndProcedure

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainButton1, 10, 10, dx - 20, 80)
  ResizeGadget(#MainButton2, 10, 100, dx - 20, dy - 110)
  UpdateImageButton(#MainButton1)
  UpdateImageButton(#MainButton2)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
    CompilerEndIf
    ; Menu File Items
    
    CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
      MenuBar()
      MenuItem(#MainMenuExit, "E&xit")
    CompilerEndIf
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    ButtonImageGadget(#MainButton1, 10, 10, dx - 20, 80, 0)
    ButtonImageGadget(#MainButton2, 10, 100, dx - 20, dy - 110, 0)
    
    ; Set image data to gadgets
    SetGadgetData(#MainButton1, @ButtonImage1)
    SetGadgetData(#MainButton2, @ButtonImage2)
    
    
    UpdateWindow()
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Event Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
            
          Case #MainMenuAbout
            MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
          Case #MainMenuExit
            PostEvent(#PB_Event_CloseWindow, #Main, #Null)
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
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
dougmo52usr
User
User
Posts: 62
Joined: Mon Jul 18, 2016 6:43 pm

Re: Get Image from ImageID

Post by dougmo52usr »

If I pass an image to a ButtonImageGadget by ImageID, then immediately free the image, the button still shows the image. For that reason, I felt that the button copied the image, and it seemed to be wasteful of memory to keep an image the button has copied. But if there is no way to request the button's copy, I will do as you say and save in gadget data.

I noticed that the image got more blurry each time, and passed a CopyImage instead, then looked in your code sample and there it was.

Thanks for the quick response,
Last edited by dougmo52usr on Wed Jul 24, 2024 9:14 pm, edited 1 time in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Get Image from ImageID

Post by mk-soft »

You can get the PB image number from the ImageID

See: Module System

But it is better to use the original image as the basic for scaling, otherwise the resolution will be lost when scaling several times.
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
Post Reply