create icon pack windows

Share your advanced PureBasic knowledge/code with the community.
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

create icon pack windows

Post by idle »

create an icon pack from a 256 x 256 png windows

Code: Select all

;make a ico pack out of a image image should be square 32 bit depth 
;Idle ChrisR

EnableExplicit 

Structure ICONDIR
  res.u
  type.u
  num.u
EndStructure

Structure ICONDIRECTORYENTRY
  width.a
  height.a
  numcolors.a 
  res.a
  colorplanes.u 
  bitsperpixel.u 
  size.l 
  offset.l  
EndStructure 

Structure ICONData
  header.ICONDIR 
  icons.ICONDIRECTORYENTRY[15] 
EndStructure 

Structure bufs
  *buffer[15] 
EndStructure   

UsePNGImageEncoder()
UsePNGImageDecoder()

; https://learn.microsoft.com/en-us/windows/apps/design/style/iconography/app-icon-construction
; Apps should have, at the bare minimum: 16x16, 24x24, 32x32, 48x48, And 256x256. 
; This covers the most common icon sizes, And by providing a 256px icon, ensures Windows should only ever scale your icon down, never up.
Procedure CreateIconFile(file.s,image,xsize.s="16x24x32x48x256") 
  Protected ico.ICONData 
  Protected buf.bufs
  Protected timg,fn,offset,a,b,countsize=CountString(xsize,"x")+1
  If countsize > 15 : countsize=15 : EndIf
  
  Dim sz(countsize-1)
  Repeat
    sz(a) = Val(StringField(xsize, a+1, "x"))
    a+1 
  Until a = countsize
  
  offset = SizeOf(ICONData)   
  ico\header\num=countsize
  ico\header\type=1 
  offset = SizeOf(ICONData)
  
  If file <> ""  
    fn = CreateFile(#PB_Any,file)
    If fn
      a = 0
      Repeat    
        timg = CopyImage(image,#PB_Any) 
        If timg 
          If ResizeImage(timg,sz(a),sz(a),#PB_Image_Smooth) 
            buf\buffer[a] = EncodeImage(timg, #PB_ImagePlugin_PNG, 0, 32)
          EndIf 
        Else 
          For b = 0 To a
            If buf\buffer[a]
              FreeMemory(buf\buffer[a])
            EndIf   
          Next 
          CloseFile(fn)
          ProcedureReturn 0
        EndIf
        FreeImage(timg) 
        ico\icons[a]\width = sz(a) 
        ico\icons[a]\height = sz(a) 
        ico\icons[a]\colorplanes = 1 
        ico\icons[a]\bitsperpixel = 32
        ico\icons[a]\size = MemorySize(buf\buffer[a]) 
        ico\icons[a]\offset = offset 
        offset + ico\icons[a]\size     
        a+1 
      Until a = countsize
      
      a=0 
      WriteData(fn,@ico,SizeOf(ICONData))   
      Repeat 
        WriteData(fn,Buf\buffer[a],ico\icons[a]\size) 
        FreeMemory(Buf\buffer[a])
        a+1
      Until a = countsize
      
      CloseFile(fn)
      ProcedureReturn 1
    EndIf  
  EndIf 
  
EndProcedure 
 
Define file$, Tcolor.l

Procedure TransparentCallback(x, y, sourcecolor.l, targetcolor.l) 
  Shared Tcolor
  If Red(sourcecolor) = Red(Tcolor) And Green(sourcecolor) = Green(Tcolor) And Blue(sourcecolor) = Blue(Tcolor)
    targetcolor = RGBA(0,0,0,0)
  Else
    targetcolor = sourcecolor
  EndIf
  ProcedureReturn targetcolor  
EndProcedure

; Example1 OpenFileRequester png Image
; file$ = OpenFileRequester("Choose icon base image", GetUserDirectory(#PB_Directory_Pictures), "png Image (*.png)|*.png", 0)
; If file$
;   LoadImage(1,file$)
;   file$ = GetPathPart(file$) + GetFilePart(file$, #PB_FileSystem_NoExtension) + ".ico"
;   End CreateIconFile(file$,1)
; Else
;   End
; EndIf

; Example2 test Image
; If CreateImage(1,512,512,32,#PB_Image_Transparent) 
;   If StartVectorDrawing(ImageVectorOutput(1))
;     VectorSourceColor(RGBA(255, 0, 0, 255))
;     AddPathCircle(512 / 2, 512 / 2, 512 / 3 )
;     FillPath()
;     StopVectorDrawing()
;   EndIf
; EndIf
; file$ = "e:\test.ico"
; End CreateIconFile(file$,1)

; Example3 Transparent geebee2.bmp Image
If LoadImage(0, #PB_Compiler_Home + "examples\sources\data\geebee2.bmp")
  If  StartDrawing(ImageOutput(0))
    Tcolor = Point(0,0)
    StopDrawing()
    CreateImage(1,ImageWidth(0),ImageHeight(0),32,#PB_Image_Transparent)
    If StartDrawing(ImageOutput(1))
      DrawingMode(#PB_2DDrawing_CustomFilter)
      CustomFilterCallback(@TransparentCallback())
      DrawImage(ImageID(0),0,0)
      StopDrawing()
    EndIf
    FreeImage(0)
  EndIf
EndIf

file$ = "e:\geebee2.ico"
CreateIconFile(file$,1)

file$ = "e:\geebee2All.ico"
CreateIconFile(file$,1,"16x24x32x48x64x96x128x256")


User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: create icon pack windows

Post by jacdelad »

Thanks idle, this we be handy.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
BarryG
Addict
Addict
Posts: 4118
Joined: Thu Apr 18, 2019 8:17 am

Re: create icon pack windows

Post by BarryG »

Thanks so much, idle!

But please note the recommended sizes, which your code doesn't create:
Microsoft wrote:Apps should have, at the bare minimum: 16x16, 24x24, 32x32, 48x48, and 256x256. This covers the most common icon sizes, and by providing a 256px icon, ensures Windows should only ever scale your icon down, never up.
Source -> https://learn.microsoft.com/en-us/windo ... nstruction

Yours does 16x16, 32x32, 64x64, 128x128, and 256x256. Can yours include 24x24 and 48x48 as well, please? Sorry! :) I did try to add them but failed, because you're resizing with "sz << 1" and I can't work around that. :oops:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4941
Joined: Sun Apr 12, 2009 6:27 am

Re: create icon pack windows

Post by RASHAD »

Hi idle
Use Vector lib. for transparency
It will cost you nothing :)
Thanks for sharing

Code: Select all

image = CreateImage(#PB_Any,512,512,32,#PB_Image_Transparent) 
StartVectorDrawing(ImageVectorOutput(image))
  VectorSourceColor(RGBA(255, 0, 0, 255))
  AddPathCircle(512 / 2, 512 / 2, 512 / 3 )
  FillPath()
StopVectorDrawing()
Egypt my love
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: create icon pack windows

Post by idle »

thanks

jacdelad
Barry
Rashad

Edited 1st post, is this right?

actually I think it could be some what improved, not sure what I was doing when I wrote it. :lol:
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: create icon pack windows

Post by Caronte3D »

Seems you forgot the 48 size?
Also... I don't know if it's normal, but when browsing the folder where the icon file is, in the Windows file manager only the first two icons (the smallest ones) are shown, when changing the view to large it doesn't show the large icon.
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: create icon pack windows

Post by idle »

Caronte3D wrote: Mon Apr 01, 2024 10:17 am Seems you forgot the 48 size?
Also... I don't know if it's normal, but when browsing the folder where the icon file is, in the Windows file manager only the first two icons (the smallest ones) are shown, when changing the view to large it doesn't show the large icon.
should be ok now 7 icons
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: create icon pack windows

Post by ChrisR »

Thanks idle, it should be useful
The icon is light 9Kb, the same icon opened then saved by IcoFx is 106Kb
I've also noticed, like Caronte3D, that the large and extra large images are not displayed in explorer, it's good for the other views.
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: create icon pack windows

Post by idle »

I'm getting the large view on win 11, generate another icon as in test1.ico, I suspect explorer is caching it as I got a green icon in the small view and red in the large.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4941
Joined: Sun Apr 12, 2009 6:27 am

Re: create icon pack windows

Post by RASHAD »

I just added 512 icon size
13 KB the size of the pack
Opened with Windows Photo viewer like a charm
Now this is the time for idle to grab a square from any image and add it to the pack :lol:
Egypt my love
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: create icon pack windows

Post by idle »

RASHAD wrote: Tue Apr 02, 2024 2:21 am I just added 512 icon size
13 KB the size of the pack
Opened with Windows Photo viewer like a charm
Now this is the time for idle to grab a square from any image and add it to the pack :lol:
I could swear it was square to being with but coding as a dyslexic is always round and about cutting corners, until it's squared away, 360 degrees then it's a circle! :lol:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4941
Joined: Sun Apr 12, 2009 6:27 am

Re: create icon pack windows

Post by RASHAD »

You remembered me with a funny Anthony Quinn when he was a stupid mafia boss and asked his friend why he turned 360 degrees against him :lol: :mrgreen:
Egypt my love
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: create icon pack windows

Post by idle »

RASHAD wrote: Tue Apr 02, 2024 4:01 am You remembered me with a funny Anthony Quinn when he was a stupid mafia boss and asked his friend why he turned 360 degrees against him :lol: :mrgreen:
Last Action hero
Tony Vivaldi : What is this, Benedict? First you're my friend; now you turn a... 360 on me!

Benedict : 180, you stupid, spaghetti-slurping cretin - *180*! If I did a 360, I'd go completely around and end up back where I started!

Tony Vivaldi : What?

Benedict : Trust me!
think I need to watch that movie again :lol:
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: create icon pack windows

Post by ChrisR »

idle wrote: Tue Apr 02, 2024 1:46 am I'm getting the large view on win 11, generate another icon as in test1.ico, I suspect explorer is caching it as I got a green icon in the small view and red in the large.
It was indeed the icon cache, I forget it every time :oops:, after rebuilding it it's all good, thanks.
User avatar
Piero
Addict
Addict
Posts: 862
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: create icon pack windows

Post by Piero »

idle wrote: Tue Apr 02, 2024 4:28 amyou stupid, spaghetti-slurping cretin
I smell racism here: may you eternally eat pineapple pizza in hell
Post Reply