Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Save icon


  • Please log in to reply
15 replies to this topic
WhyDoesNobodyEverHelp?
  • Guests
  • Last active:
  • Joined: --
Hello

Could someone please answer? Everytime I post something - it just sits empty :(

I found this on your site:

FileName = %A_AhkPath%
Loop 5 
   Gui, Add, Picture, w16 h16 x+0 Icon%A_Index%, %FileName% 
Gui, Show 
return

Is it possible to extract the icon from a file and save it to your hard-drive?

Thank you in advance... just incase someone takes pitty on me this time

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
The code you quote loads an icon into an AutoHotkey GUI. If you want to extract an icon from a resource yourself, you'd need other code, or you could go the easy route, and use a program like Resource Hacker.

HTH

PS: If you don't get an answer, bump your original post instead of posting a new thread.

Z Gecko
  • Guests
  • Last active:
  • Joined: --
Hi,

Could someone please answer? Everytime I post something - it just sits empty

I don´t know your other questions, but this one is very unclear. Maybe your other questions were similar.
Most people don´t like to search for the true meaning of questions, if they don´t understand them they leave them out and go to the next one.

So, to clearify:

1. Precise your question! What icon do u want to"save on your hard-drive"? & Do you want to do it with ahk, or manually?

2. Describe your intention. There is often more than one way, if we know your goal, we might come up with something else, that suits you even better.

3. Please post YOUR hole code (the snippet alone is quite useless)

WhyDoesNobodyEverHelp?
  • Guests
  • Last active:
  • Joined: --
Thank you for replying :D

I just wanted AHK to save an icon from an exe.

Like resource hacker - but I only want the icon, and I would like to do it using AHK

Thank you again - I am very grateful :D

Z Gecko
  • Guests
  • Last active:
  • Joined: --

I just wanted AHK to save an icon from an exe.

To do this with ahk is at least not easy, if it´s possible at all.
Since you can not simply display the icon in a gui and then capture it (because you would destroy the transparency),
you would need some bit-wizzard to make it work.
Therefore i would suggest to search for a command-line-tool that can do this and just use it from ahk.

But why exactly is it neccessary to do this at all?
Just to make shure, you really need this behavior.

Z Gecko
  • Guests
  • Last active:
  • Joined: --

Therefore i would suggest to search for a command-line-tool ...

http://www.bestfreew...e-dtjvefuq.html

WhyDoesNobodyEverHelp?
  • Guests
  • Last active:
  • Joined: --
Thank you

I just wanted to play around with AHK a bit - and wondered if it would do this.

If the answer is no - then that's OK - you have saved me hours of messing to find out that it doesn't do it.

Thank you again - I appreciate your time

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
I havent figured out how to save it as an icon yet. it seems a bit confusing and there only seem to be VB examples. I can save to any image format though...

<!-- m -->http://www.news2news...fp/?example=502<!-- m -->

MI_ExtractIcon is Majikineter's function

#SingleInstance, Force
#NoEnv
DetectHiddenWindows, On
CoordMode, Mouse, Screen
SetBatchLines, -1
SetWinDelay, 0
SetWorkingDir %A_ScriptDir%

OnExit, Exit

Filename = test.exe
IconNumber = 1
IconSize = 128
OutPutFile = test.png

pToken := Gdip_Startup()
hIcon := MI_ExtractIcon(Filename, IconNumber, IconSize)
pBitmap := Gdip_CreateBitmapFromHICON(hIcon)

DestroyIcon(hIcon)
Gdip_DisposeImage(pBitmap)
GoSub, Exit
Return

;#####################################################################################################

Exit:
Gdip_Shutdown(pToken)
ExitApp
Return

;#####################################################################################################

Gdip_Startup()
{
	If !DllCall("GetModuleHandle", "Str", "gdiplus")
	DllCall("LoadLibrary", "Str", "gdiplus")
	VarSetCapacity(si, 16, 0), si := Chr(1)
	DllCall("gdiplus\GdiplusStartup", "UInt*", pToken, "UInt", &si, "UInt", 0)
	VarSetCapacity(si, 0)
	Return, pToken
}

Gdip_Shutdown(pToken)
{
	DllCall("gdiplus\GdiplusShutdown", "UInt", pToken)
	If hModule := DllCall("GetModuleHandle", "Str", "gdiplus")
	DllCall("FreeLibrary", "UInt", hModule)
	Return, 0
}

Gdip_CreateBitmapFromHICON(hIcon)
{
	DllCall("gdiplus\GdipCreateBitmapFromHICON", "UInt", hIcon, "UInt*", pBitmap)
	Return, pBitmap
}

MI_ExtractIcon(Filename, IconNumber, IconSize)
{
    If A_OSVersion in WIN_VISTA,WIN_2003,WIN_XP,WIN_2000
    {
		DllCall("PrivateExtractIcons", "Str", Filename, "Int", IconNumber-1, "Int", IconSize, "Int", IconSize, "UInt*", hIcon, "UInt*", 0, "UInt", 1, "UInt", 0, "Int")
        If !ErrorLevel
        Return hIcon
    }
    If DllCall("shell32.dll\ExtractIconExA", "Str", Filename, "Int", IconNumber-1, "UInt*", hIcon, "UInt*", hIcon_Small, "UInt", 1)
    {
        SysGet, SmallIconSize, 49
        
        If (IconSize <= SmallIconSize)
		{
			DllCall("DeStroyIcon", "UInt", hIcon)
			hIcon := hIcon_Small
        }
		Else
		DllCall("DeStroyIcon", "UInt", hIcon_Small)
        
        If (hIcon && IconSize)
		hIcon := DllCall("CopyImage", "UInt", hIcon, "UInt", 1, "Int", IconSize, "Int", IconSize, "UInt", 4|8)
    }
    Return, hIcon ? hIcon : 0
}

DestroyIcon(hIcon)
{
	Return, DllCall("DestroyIcon", hIcon)
}

Gdip_DisposeImage(pImage)
{
	Return, DllCall("gdiplus\GdipDisposeImage", "UInt", pImage)
}

Gdip_SaveBitmapToFile(pImage, sOutput)
{
    StringSplit, OutputArray, sOutput, .
    Extension := "." . OutputArray%OutputArray0%
	If Extension not in .png,.bmp,.jpg,.tiff,.gif
	Return, -1
	
	DllCall("gdiplus\GdipGetImageEncodersSize", "UInt*", nCount, "UInt*", nSize)
    VarSetCapacity(ci, nSize)
    DllCall("gdiplus\GdipGetImageEncoders", "UInt", nCount, "UInt", nSize, "UInt", &ci)
	If !(nCount && nSize)
	Return, -5
	
    Loop, %nCount%
    {
        nSize := DllCall("WideCharToMultiByte", "UInt", 0, "UInt", 0, "UInt", NumGet(ci, 76*(A_Index-1)+44), "Int", -1, "UInt", 0, "Int",  0, "UInt", 0, "UInt", 0)
        VarSetCapacity(sString, nSize)
        DllCall("WideCharToMultiByte", "UInt", 0, "UInt", 0, "UInt", NumGet(ci, 76*(A_Index-1)+44), "Int", -1, "Str", sString, "Int", nSize, "UInt", 0, "UInt", 0)
   
        If !InStr(sString, Extension)
        Continue
        pCodec := &ci+76*(A_Index-1)
        Break
    }
	If !pCodec
	Return, -6
	
	nSize := DllCall("MultiByteToWideChar", "UInt", 0, "UInt", 0, "UInt", &sOutput, "Int", -1, "UInt", 0, "Int", 0)
	VarSetCapacity(wOutput, nSize*2)
	DllCall("MultiByteToWideChar", "UInt", 0, "UInt", 0, "UInt", &sOutput, "Int", -1, "UInt", &wOutput, "Int", nSize)
	VarSetCapacity(wOutput, -1) 
	If !VarSetCapacity(wOutput)
	Return, -7

	E := DllCall("gdiplus\GdipSaveImageToFile", "UInt", pImage, "UInt", &wOutput, "UInt", pCodec, "UInt", 0)
	Return, E ? -8 : 0
}

Edit:

I just updated the Gdip_SaveBitmapToFile function with error checking to be compliant with my previously released function ConvertImage:

http://www.autohotke...pic.php?t=31456

Z Gecko
  • Guests
  • Last active:
  • Joined: --
very cool, tic

WhyDoesNobodyEverHelp?
  • Guests
  • Last active:
  • Joined: --
Wow - that is cool

I am sorry about stating that people don't help.

You have been very helpfool - and very impressive !!!

Thank you

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
After doing some reading I can see all the problems:

<!-- m -->http://www.codeproje.../MultiIcon.aspx<!-- m -->

<!-- m -->http://www.codeproje...s/safeicon.aspx<!-- m -->

I had previously created icons a long time ago:

<!-- m -->http://www.autohotke... ... 825#161825<!-- m -->

So I could easily create an icon by creating a bmp in memory from the handle to the icon and then creating a .ico from that information and write it using a new header and tail, but that is quite a messy way :? and would not contain multiple icon sizes.....

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

I am sorry about stating that people don't help.


Then why not use a less offending nick ? Anyways! Something is better rather than not using a nick.

You have been very helpfool - and very impressive !!!


Oh my! I hope that was a typo :(






@ Z Gecko / tic

Friends, it seems possible with regular API to extract an icon based on the Icon Group number. An Icon Group would be having similar icons in various size/bits and re-creating the header would be a bit complicated. I gave a try with AutoHotkey.exe's Icon Group ( because it has only a single icon per group ) and so, re-creating the header is very simple:

The following code extracts Icon Group - 159 from AutoHotkey.exe

; MSDN : Icons in Win32 --->         http://msdn.microsoft.com/en-us/library/ms997538.aspx

hModule   := DllCall( "GetModuleHandle", Str, A_AhkPath )       ; Handle to AutoHotkey.exe 

Buffer := GetResource( hModule, [color=red]159[/color], (RT_GROUP_ICON:=14), nSize ) 
h := DllCall( "_lcreat", Str,"ahk.ico", UInt,0 )                ; writing the file header 
DllCall( "_lwrite", UInt,h, UInt,Buffer, UInt,18 )
VarSetCapacity( off,4,0), NumPut( 22, off )   
DllCall( "_lwrite", UInt,h, Str,off, UInt,4 )                   ; hardcoding the offset

nID := NumGet( Buffer+0, 18, "UShort")
Buffer := GetResource( hModule, nID, (RT_ICON:=3), nSize )
DllCall( "_lwrite", UInt,h, UInt,Buffer, UInt,nSize )           ; writing the icon data
DllCall( "_lclose", UInt,h )

GetResource( hModule, rName, rType, ByRef nSize ) {
  hResource := DllCall("FindResource", UInt,hModule, UInt,rName, UInt,rType ) 
  nSize     := DllCall("SizeofResource", UInt,hModule, UInt, hResource)
  hResData  := DllCall("LoadResource", UInt,hModule, UInt,hResource ) 
Return DllCall("LockResource", UInt, hResData )
}

:)

WhyDoesNobodyEverHelp?
  • Guests
  • Last active:
  • Joined: --
Yes it was a typo - I really am greatful.

Also - when I used the name, I didn't expect anyone to answer, as I have not received any replies in the past.

I did not mean to offend

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Thanks Skan. Any idea how to recreate the header if there are many sizes? (it would be useful to have a complete breakdown of the header)

Also I personally wouldnt use _lcreat etc:

[This function is provided for compatibility with 16-bit versions of Windows. New applications should use the CreateFile function.]


ps. Not having a go Skan, but do you think that perhaps you could try to write your code a tiny bit neater? It's just that it's sometimes hard for me to read, and I must edit it so that I may read it. Honestly I'm not saying anything bad! It would just help me as something like this is impossible to read:

<!-- m -->http://www.autohotke...pic.php?t=29593<!-- m -->

Thanks dude!

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Thanks Skan. Any idea how to recreate the header if there are many sizes? (it would be useful to have a complete breakdown of the header)


I am working on it .. studying the .ICO structure.. looks very much doable. :)

Also I personally wouldnt use _lcreat etc:

[This function is provided for compatibility with 16-bit versions of Windows. New applications should use the CreateFile function.]


MSDN also recommends to use Registry over INI files. That would be for C programmers, as Microsoft would not be aware of AHK world. ;)

Seriously speaking, the main criteria for me is: SetFilePointer() Vs _llseek()
While _llseek() is as simple as SubStr(), the former is unneccesarily complicated.

Also these 16bit IO functions work in every Windows OS version available ( including 64bit OS, as I learnt from Sarah )

Only drawback is that these functions do not have proper documentation. I was wondering if I could post enough examples before hand, then I could post a tutorial on these simple yet powerful functions.

ps. Not having a go Skan, but do you think that perhaps you could try to write your code a tiny bit neater? It's just that it's sometimes hard for me to read, and I must edit it so that I may read it. Honestly I'm not saying anything bad! It would just help me as something like this is impossible to read:

<!-- m -->http://www.autohotke...pic.php?t=29593<!-- m -->


:D .. I agree that was too wild and will refrain from such experiments in future.