Get icon from an EXE and use it like CatchImage()?

Windows specific forum
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Get icon from an EXE and use it like CatchImage()?

Post by jassing »

I thought I had code saved around my disk; but I can't find it; and can't find an email or forum post that fits.

I need to grab the icon of an exe, and then use it as an image in PB. You can include an ico, then use CatchImage() -- I would like to do something like

Code: Select all

CatchExeImage(#NotePadIcon, "C:\windows\system32\notepad.exe", 0) ; Where 0 is the icon index.
This is (currently) a windows-only problem, so I don't mind apis, I have failed with ExtractIcon() and ExtractIconEx() to get them to move the icon into a image # (ImageID()?) that I can then use in pb.
Last edited by jassing on Sat Jun 01, 2013 3:33 pm, edited 1 time in total.
User avatar
Bisonte
Addict
Addict
Posts: 1320
Joined: Tue Oct 09, 2007 2:15 am

Re: Get EXE from icon and use it like CatchImage()?

Post by Bisonte »

Code: Select all

; Get the Icon from exe or dll
hIcon = ExtractIcon_(#Null, "C:\windows\write.exe", 0)

; the hIcon is an os-handle, its the ImageID.

; Create a new image
Image = CreateImage(#PB_Any, 32, 32)

; draw the icon on it
hDc = StartDrawing(ImageOutput(Image))
If hDc
  Box(0,0,32,32,0)
  DrawIcon_(hDc, 0, 0, hIcon)
  StopDrawing()
EndIf

; Free Up 
DestroyIcon_(hIcon)

; save the image or whatever you want...
SaveImage(Image, "D:\aa.bmp")
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Get EXE from icon and use it like CatchImage()?

Post by jassing »

Thank you! That is so simple... Thank you very much.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Get EXE from icon and use it like CatchImage()?

Post by IdeasVacuum »

That's an excellent tip Bisonte 8)

jassing - could you change the title of your post to make it more likely to be found in a search? At the moment it doesn't match the post at all - you want the icon from an exe, not an exe from an icon..........
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Get EXE from icon and use it like CatchImage()?

Post by jassing »

even I was surprised on a new day that I had it backwards. I'm glad someone A) knew what I meant and B) poked me to change the title.

well the icon isn't being saved properly -- as an example:
icon in the exe: Image

two icons extracted using code above:
Image Image
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Get icon from an EXE and use it like CatchImage()?

Post by IdeasVacuum »

Try:

Code: Select all

; Create a new image
Image = CreateImage(#PB_Any, 32, 32, 32|#PB_Image_Transparent)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Get icon from an EXE and use it like CatchImage()?

Post by jassing »

same end result, although file sizes were different.
thanks. Perhaps I need to set "black" as the transparent colour?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Get icon from an EXE and use it like CatchImage()?

Post by IdeasVacuum »

...by default, it is :shock:

Edit: Drawing Mode could be it.......

Edit2: Draw the Box using RGBA(0,0,0,0) (Fully Transparent)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4997
Joined: Sun Apr 12, 2009 6:27 am

Re: Get icon from an EXE and use it like CatchImage()?

Post by RASHAD »

Hi jassing
- Extract all exe icons in one step ( Small & Large )
- Save Icons with transparency ( PNG format supported )

Code: Select all

UsePNGImageDecoder()
UsePNGImageEncoder()

File$ = #PB_Compiler_Home+"purebasic.exe"

num = ExtractIconEx_(File$, -1, 0, 0, 0)

If num > 0
  Dim SIcon(num)
  Dim LIcon(num)
  
  ExtractIconEx_(File$, 0, LIcon(), SIcon(), num)
  
  OpenWindow(0,0,0,640,480,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  CreateImage(0,32,32, 32)
  ImageGadget(0,10,10,32,32,0)
  
  StartDrawing(ImageOutput(0))
  DrawingMode(#PB_2DDrawing_AllChannels)
  DrawImage(LIcon(0),0,0,32,32)
  StopDrawing()
  
  SetGadgetState(0,ImageID(0))
  SaveImage(0, "e:\test.png",#PB_ImagePlugin_PNG)
   
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
           Quit = 1
      Case #PB_Event_Gadget
  
    EndSelect 
  Until Quit = 1
EndIf
Egypt my love
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Get icon from an EXE and use it like CatchImage()?

Post by jassing »

Works Perfectly! Thank you Rashad!
Post Reply