Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

05 Sep 2017, 08:05

Should be also possible with ComObjCreate("Msxml2.DOMDocument"), createElement("base64") and dataType := "bin.base64"
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

05 Sep 2017, 09:03

Code: Select all

hICON := b64Decode(PNG10x10, 48, 48)

b64Decode(b64, w := 16, h := 16) ; borrowed from uberi
{
    static CharSet := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
    VarSetCapacity(bin, Ceil(((len := StrLen(b64)) / 4) * 3), 0), i := 1, p := 0
    loop % len // 4 {
        b := ((InStr(CharSet, SubStr(b64, i,     1), true) - 1) << 0x12)
           | ((InStr(CharSet, SubStr(b64, i + 1, 1), true) - 1) << 0x0c)
           | ((InStr(CharSet, SubStr(b64, i + 2, 1), true) - 1) << 0x06)
           | ((InStr(CharSet, SubStr(b64, i + 3, 1), true) - 1)        )
        NumPut((b >> 0x10) | (((b >> 0x08) & 0xff) << 0x08) | ((b & 0xff) << 0x10), bin, p, "uint")
        i += 4, p += 3
    }
    if (len & 3) {
        b := ((InStr(CharSet, SubStr(b64, i, 1), true) - 1) << 0x12) | ((InStr(CharSet, SubStr(b64, i + 1, 1), true) - 1) << 0x0c)
        NumPut(b >> 0x10, bin, p, "uchar")
        if (len & 1) {
            b |= ((InStr(CharSet, SubStr(b64, i + 2, 1), true) - 1) << 0x06)
            NumPut((b >> 0x08) & 0xff, bin, p + 1, "uchar")
        }
    }
    return DllCall("CreateIconFromResourceEx", "ptr", &bin, "uint", len, "int", true, "uint", 0x30000, "int", W, "int", H, "uint", 0, "uptr") 
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

05 Sep 2017, 13:36

SKAN wrote: Its my policy. I will specifically mention, if my function is not meant for XP.
:salute:
SKAN wrote:A perfect one-liner to avoid the additional DllCall() would be a good improvement for that function.
round(strlen(B64)/4*3) or round(strlen(strReplace( B64,"=","=",e))/4*3-e), either will work, I prefer the first. I wouldn't do the floor, I'd worry about potentally rounding down exactSize-0.0000000000x to exactSize-1 when there is no padding.
I guess you eliminate the size calculation dllcall for the Base64Enc function too, nChars:=ceil(nBytes/4*3)+linebreaks+spaces. :?:

@ jNizM, hi and interesting code :wave: (My guess is your nick is the most copy-pasted on the forum, I can't remeber how to spell it at least :D )

Cheers.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

06 Sep 2017, 12:02

@ jNizM
Thanks for sharing :)
PS: I miss uberi.
Helgef wrote:round(strlen(B64)/4*3) or round(strlen(strReplace( B64,"=","=",e))/4*3-e), either will work, I prefer the first.
Thank you. I will test it and reply. :)
My Scripts and Functions: V1  V2
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Base64PNG_to_HICON() - Updated

07 May 2020, 14:00

I wrote:
Helgef wrote:round(strlen(B64)/4*3) or round(strlen(strReplace( B64,"=","=",e))/4*3-e), either will work, I prefer the first.
Thank you. I will test it and reply. :)
I tested well. round(strlen(strReplace( B64,"=","=",e))/4*3-e) works well :thumbup: round(strlen(B64)/4*3) doesn't.

I've updated the code with this slightly modified floor version from @jeeswg : bytes := floor(strlen(RTrim( B64,"="))*3/4)
My Scripts and Functions: V1  V2
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

PNG_to_HICON()

22 May 2020, 15:57

I was searching for something else and landed on this ask-for-help topic
LoadPicture to use it as icon by @john_c

So, here is the modified version of Base64PNG_to_HICON() which
loads ICON from a PNG file.

Code: Select all

PNG_to_HICON(File, W:=0, H:=0) { ; By SKAN 
FileRead, Bin ,*c %File%
Return DllCall("CreateIconFromResourceEx", "Ptr",&Bin, "UInt",VarSetCapacity(Bin)
             , "Int",True, "UInt",0x30000, "Int",W, "Int",H, "UInt",0, "UPtr")             
}

; DEMO

#NoEnv
#Warn
#SingleInstance, Force
SetWorkingDir %A_ScriptDir%

UrlDownloadToFile, https://www.autohotkey.com/assets/images/ahk-logo-no-text241x78-180.png, ahk.png
Gui, Add, Picture,, % "HICON:" PNG_to_HICON("ahk.png")
Gui, Show
Return
My Scripts and Functions: V1  V2
Gewerd_Strauss
Posts: 24
Joined: 12 Nov 2020, 02:34

Re: Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

06 Apr 2023, 14:52

Hello @SKAN ,

this is a nice and very useful little function which has found use in a variety of my projects,

However I wondered if you decided whether or not to license it. There are a couple of scripts I wrote to make my workload during my internship easier, and some of those my workplace has shown interest in keeping, and thus I am now in the process of checking for licenses or figuring out how to gut and replace snippets.

Sincerely,
~Gw
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

13 Apr 2023, 21:50

Hi @Gewerd_Strauss,
I wondered if you decided whether or not to license it.
My code uses pd-self license.

:)
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Base64PNG_to_HICON() for AHK v2

04 Jun 2023, 04:30

Code: Select all

Base64PNG_to_HICON(Base64PNG, W:=0, H:=0)      ;  By SKAN for ah2 on D094/D664 @ autohotkey.com/r?p=524691
{
    Local  BLen   :=  StrLen(Base64PNG)
        ,  nBytes :=  Floor( StrLen(RTrim(Base64PNG,"=")) * 3/4 )
        ,  Bin    :=  Buffer(nBytes)

    Return  DllCall("Crypt32\CryptStringToBinary", "str",Base64PNG, "uint",BLen, "uint",1, "ptr",Bin
                                                 , "uintp",nBytes,  "uint",0, "uint",0)
         ?  DllCall("User32\CreateIconFromResourceEx", "ptr",Bin, "uint",nBytes, "int",True
                                                     , "uint",0x30000, "int",W, "int",H, "uint",0, "uptr")
         :  0
}


#Requires AutoHotkey v2.0
#Warn
#SingleInstance

Base64PNG := "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAAAflBMVEXOgwD///+AUQDz5NSTXQDj3NSliWe7dwCnagD"
. "GtaPnx6Pbp2eGVQDt1rz6+PXRjSTJgADCewCycQCeZADkwJbhuIf58ur06t/qzrDesHirbQDw3ci0nILYn1TVlj+KYSSiZwCYYQCOW"
. "gDVyby+q5acfFSTbz/u4dTc08jNv7D3Mcn0AAACq0lEQVR42uzaXW/aMBSA4WMn4JAQyAff0A5o123//w/OkSallUblSDm4qO9759z"
. "Yfo4vI0RERERERERERERERERERERERB97Kva5L3lX6deroljKXVoWxcpvWCbv2vkP++JJdFvud8nCfFZSrlQP8bwqE/NZiyTfa82hO"
. "JqgNrkotd6YoI6FKFSa4LYqM1huTXCljN7aGIX9dSbgW8vYJWZIopAZUgIAAADEBHCuigvwy9VRAawvbQ91NICJP8A8zZoqIkDXPIs"
. "G8K+Liwngu1ZRAXxtXADbxgawTVwAGx0gBQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
. "AAAAAAAAAAAAAAAAAAAAAAAAADgI8BDBQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD6AFOFHgrAKgQAAAAAAAA"
. "AADwegBuphwX4ln+KAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
. "AAAAAAAAAAAAAAAAPA1AY5mQAsNgIUZ0O/RAQozoJkGQG4GNB0dQNbhE/hjNQBkF/4CT3Z8AFmutkGbv/y0OgDyvNuYgLavP6wGQGd"
. "Q5GVy+xCTyezU3V4LoDNY50lyG3/yMpt2t1cB6EunvtOsr1u/2RuJQm9T36zv1S/7m+sD2CGJQva/AQDAnQAudkBzUWhuB3SRsXN2Q"
. "JkolNkBORm9JnwCZ1HpHP4CG1GoOlyDNm9rUao+Bw3heqhEqcplbXr7EGmaNbWoVjdZmt7GT9vMVaKf8zVZn/PVcsdq58v6Ds5XCRE"
. "RERER/W0PDgkAAAAABP1/bfQEAAAAAAAL2VmKC7LwdTIAAAAASUVORK5CYII="

MyGui1 := Gui("-MinimizeBox","Original size 256x256")
MyGui1.AddPicture(,"HICON:" Base64PNG_to_HICON(Base64PNG))
MyGui1.Show("x10 y10")

MyGui2 := Gui("-MinimizeBox","Enlarged and non-square 400x300")
MyGui2.AddPicture(,"HICON:" Base64PNG_to_HICON(Base64PNG, 400, 300))
MyGui2.Show()

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 130 guests