Extract icon given the file path.

BlitzMax Forums/BlitzMax Programming/Extract icon given the file path.

Nate the Great(Posted 2011) [#1]
Hi, Its been a long time since i posted here... Anyway I was wondering if I can somehow get the image for the icon of a file if all I know is the file path. Some things, like blitz max files, only show up as a bmx icon because their default program is blitz max but some files (executables) seem to have the icon as a seperate image (an icon file) and yet others seem to have it mysteriously embedded into them (there is no icon file) Is there a standard simple way to extract or even display these icons via max gui or is there some format I need to know so I can program an icon extractor/displayer?


xlsior(Posted 2011) [#2]
Executables can have embedded resources like icons which can be contained inside the .exe. In order to view these, you'd need to look at the win32 .exe fileformat descriptions in order to analyze the .exe to find the embedded resource to extract the info from.
As far as I know, only executable files (.exe, .dll, scr, etc.) will have their own icon embedded in the actual file itself.

Filetype handlers are stored in the registry.
You can see a list of all extensions that are registered with the system in regedit, under HKEY_CLASSES_ROOT.

For example, under HKEY_CLASSES_ROOT, I show an extension for "8bf", which it defined as "photoshop.plugin".
Then I can look under "HKEY_CLASSES_ROOT\Photoshop.Plugin\DefaultIcon", and see a (default) that points to "C:\program files\adobe\photoshop elements 8.0\photoshopelementseditor.exe,2"

This means that a .8bf file uses the *second* included resource icon in the photoshopelementseditor.exe file.

There is code in the code archives that allows BlitzMax to read and parse the windows registry, but you're still going to have to figure out how to read the embedded resource data from the .exe's to extract the actual icon...

Good luck. :-?


ima747(Posted 2011) [#3]
I've been meaning to dig into this for years (also extracting image thumbnail icons that windows and macOS generate etc.). If any progress is made I'd love to see a post... seems like it could make a handy module, one that could extract the icon the OS uses for a gives file/folder/picture/application/etc.


Nate the Great(Posted 2011) [#4]
@xlsior Dang, that sounds complicated :P maybe there is a win api function to do it? The registry thing builds on the first part so it wont be practical until I can extract the icon from the exe already.

this looks interesting, not sure how to use it though..

http://wiki.tcl.tk/17859

Last edited 2011


BlitzSupport(Posted 2011) [#5]
There's a Win32 API function called ExtractAssociatedIcon that'll get the raw icon data from any file, but getting the icon into an image you can display is another matter.

The ExtractAssociatedIcon parameters are:

· hInst - your program; pass Win32 API function GetModuleHandle with Null as parameter.

· lpIconPath - path to file from which to get icon (a pointer to a C string, so you need to pass a Varptr to a byte array containing the file name);

· lpiIcon - Varptr to an integer set to zero (or just a Byte Ptr?)

Drawing it isn't as easy (it is if you're drawing directly to a Win32 GUI element since you just call Win32's DrawIcon, but you're probably not doing that). This gives an idea as to how to get the bitmap...

http://www.programmersheaven.com/mb/windows/74164/74164/converting-icon-to-bitmap-hicon---hbitmap/


Nate the Great(Posted 2011) [#6]
they seem to use a createcompatiblebitmap function in the second link... I dont see how that would help in bmax lol :p maybe theres a file format out there for it...