Page 1 of 1
Get icon from an EXE and use it like CatchImage()?
Posted: Sat Jun 01, 2013 12:54 am
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.
Re: Get EXE from icon and use it like CatchImage()?
Posted: Sat Jun 01, 2013 2:01 am
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")
Re: Get EXE from icon and use it like CatchImage()?
Posted: Sat Jun 01, 2013 3:40 am
by jassing
Thank you! That is so simple... Thank you very much.
Re: Get EXE from icon and use it like CatchImage()?
Posted: Sat Jun 01, 2013 12:12 pm
by IdeasVacuum
That's an excellent tip Bisonte
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..........
Re: Get EXE from icon and use it like CatchImage()?
Posted: Sat Jun 01, 2013 3:34 pm
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:
two icons extracted using code above:

Re: Get icon from an EXE and use it like CatchImage()?
Posted: Sat Jun 01, 2013 4:44 pm
by IdeasVacuum
Try:
Code: Select all
; Create a new image
Image = CreateImage(#PB_Any, 32, 32, 32|#PB_Image_Transparent)
Re: Get icon from an EXE and use it like CatchImage()?
Posted: Sat Jun 01, 2013 6:02 pm
by jassing
same end result, although file sizes were different.
thanks. Perhaps I need to set "black" as the transparent colour?
Re: Get icon from an EXE and use it like CatchImage()?
Posted: Sat Jun 01, 2013 7:10 pm
by IdeasVacuum
...by default, it is
Edit: Drawing Mode could be it.......
Edit2: Draw the Box using RGBA(0,0,0,0) (Fully Transparent)
Re: Get icon from an EXE and use it like CatchImage()?
Posted: Sat Jun 01, 2013 7:22 pm
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
Re: Get icon from an EXE and use it like CatchImage()?
Posted: Sun Jun 02, 2013 4:53 pm
by jassing
Works Perfectly! Thank you Rashad!