[SOLVED] Select and load specific image from icon

Just starting out? Need help? Post your questions and find answers here.
troy
User
User
Posts: 51
Joined: Sat May 31, 2003 2:59 pm

[SOLVED] Select and load specific image from icon

Post by troy »

Hi,

As you know the icon file can contain several images of different sizes inside. Is it possible to load a specific image from the icon in PB?
Last edited by troy on Tue Apr 11, 2017 12:18 pm, edited 1 time in total.
--
troy
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Select and load specific image from icon

Post by IdeasVacuum »

This might help (PB Tips n Tricks):
Extract Icon from exe,dll,ico,cur,bmp
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Select and load specific image from icon

Post by Josh »

Windows only and for ToolBar

Create a resourcefile Icons.rc with following content:

Code: Select all

MyIco ICON "D:\AnyIco.ico"
Add this resourcefile in CompilerOptions and run the following code:

Code: Select all

 ;Open Window
  nWindow = OpenWindow(#PB_Any, 100, 100, 200, 200, "ToolBar Example")
  hWindow = WindowID (nWindow)

 ;Add ToolBar
  nToolBar = CreateToolBar (#PB_Any, hWindow)
  hToolBar = ToolBarID (nToolBar)

 ;Change ToolBar size
  hImageList = SendMessage_(hToolbar, #TB_GETIMAGELIST, 0, 0)
  ImageList_SetIconSize_(hImageList, 48, 48)
  SendMessage_(hToolbar, #TB_SETIMAGELIST, 0, hImageList)
  SendMessage_(hToolbar, #TB_AUTOSIZE, 0, 0)

 ;Add ToolBarButtons
  ToolBarImageButton (0, LoadIcon_(GetModuleHandle_(0), "MyIco"))
  ToolBarSeparator ()

  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
sorry for my bad english
troy
User
User
Posts: 51
Joined: Sat May 31, 2003 2:59 pm

Re: Select and load specific image from icon

Post by troy »

Thanks guys for pointing me out. I've managed it this way:

Code: Select all

#ico_size = 16
#ico_bpp = 32
#ico_file = "c:\anyicon.ico"

Structure ICONDIRENTRY
   bWidth.a       ; Width, in pixels, of the image
   bHeight.a      ; Height, in pixels, of the image
   bColorCount.a  ; Number of colors in image (0 if >=8bpp)
   bReserved.a    ; Reserved ( must be 0)
   wPlanes.w      ; Color Planes
   wBitCount.w    ; Bits per pixel
   dwBytesInRes.l ; How many bytes in this resource?
   dwImageOffset.l; Where in the file is this image?
EndStructure

Structure ICONDIR
   idReserved.w ; Reserved (must be 0)
   idType.w     ; Resource Type (1 for icons)
   idCount.w    ; How many images?
   Array idEntries.ICONDIRENTRY(1) ;An entry for each image (idCount of 'em)
EndStructure

;window
nWindow = OpenWindow(#PB_Any, 100, 100, 200, 200, "Select 16x16 image from the icon example")
hWindow = WindowID(nWindow)

;toolbar
nToolBar = CreateToolBar(#PB_Any, hWindow)
hToolBar = ToolBarID(nToolBar)

;change toolbar size
;hImageList = SendMessage_(hToolbar, #TB_GETIMAGELIST, 0, 0)
;ImageList_SetIconSize_(hImageList, #ico_size, #ico_size)
;SendMessage_(hToolbar, #TB_SETIMAGELIST, 0, hImageList)
;SendMessage_(hToolbar, #TB_AUTOSIZE, 0, 0)

;load ordinary icon
icon1 = LoadImage(#PB_Any, #ico_file)

;load choosen icon
Define ico.ICONDIR
f = ReadFile(#PB_Any, #ico_file)
If f
    
  ;get ICONDIR data
  ico\idReserved = ReadWord(f)
  ico\idType = ReadWord(f)
  ico\idCount = ReadWord(f)
    
  ;Debug ico\idReserved
  ;Debug ico\idType
  ;Debug ico\idCount
  
  ReDim ico\idEntries(ico\idCount - 1)
  
  ;get ICONDIRENTRY data
  If ReadData(f, @ico\idEntries(0), SizeOf(ICONDIRENTRY) * ico\idCount)
    
    For n = 0 To ico\idCount - 1
      
      Debug "CurIcon >"+Str(ico\idEntries(n)\bWidth)+"<>"+Str(ico\idEntries(n)\bHeight)+"<>"+Str(ico\idEntries(n)\wBitCount)+"<"
      
      If ico\idEntries(n)\bWidth = #ico_size And ico\idEntries(n)\bHeight = #ico_size And ico\idEntries(n)\wBitCount = #ico_bpp
        
        ;get desired image from the icon
        *img_data = AllocateMemory(ico\idEntries(n)\dwBytesInRes)
        If *img_data
          
          FileSeek(f, ico\idEntries(n)\dwImageOffset)
          If ReadData(f, *img_data, ico\idEntries(n)\dwBytesInRes)
            
            ;make a single icon file in memory
            pointer = 0
            *img = AllocateMemory(ico\idEntries(n)\dwBytesInRes + 22)
            If *img
              
              ;set ICONDIR data
              PokeW(*img, ico\idReserved)
              pointer + 2
              PokeW(*img + pointer, ico\idType)
              pointer + 2
              PokeW(*img + pointer, 1) ;keep only one icon now
              pointer + 2
            
              ;set ICONDIRENTRY data
              PokeA(*img + pointer, ico\idEntries(n)\bWidth)
              pointer + 1
              PokeA(*img + pointer, ico\idEntries(n)\bHeight)
              pointer + 1
              PokeA(*img + pointer, ico\idEntries(n)\bColorCount)
              pointer + 1
              PokeA(*img + pointer, ico\idEntries(n)\bReserved)
              pointer + 1
              PokeW(*img + pointer, ico\idEntries(n)\wPlanes)
              pointer + 2
              PokeW(*img + pointer, ico\idEntries(n)\wBitCount)
              pointer + 2
              PokeL(*img + pointer, ico\idEntries(n)\dwBytesInRes)
              pointer + 4
              PokeL(*img + pointer, 22)
              pointer + 4
            
              ;merge meta and data
              CopyMemory(*img_data, *img + 22, ico\idEntries(n)\dwBytesInRes)
            
              ;load icon from memory
              icon2 = CatchImage(#PB_Any, *img)
              
              FreeMemory(*img)
              
            EndIf
            
          EndIf  
          
          FreeMemory(*img_data)  
          
        EndIf  
        
      EndIf  
      
    Next  
    
  EndIf  
    
  CloseFile(f)
  
EndIf

;add toolbar buttons
ToolBarImageButton(0, ImageID(icon1)) ;ordinary icon
ToolBarSeparator()
ToolBarImageButton(0, ImageID(icon2)) ;choosen icon

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
--
troy
Post Reply