Base64ToComByteArray() :: Include image in script and display it with WIA 2.0

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Base64ToComByteArray() :: Include image in script and display it with WIA 2.0

21 Aug 2017, 16:51

The function:

Code: Select all

Base64ToComByteArray( ByRef B64 ) {  ; By SKAN / Created: 21-Aug-2017 / Topic: goo.gl/dyDxBN
Static CRYPT_STRING_BASE64 := 0x1
Local  Rqd := 0, BLen := StrLen(B64), ByteArray := ""  

  If DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",CRYPT_STRING_BASE64
              , "UInt",0, "UIntP",Rqd, "Int",0, "Int",0 )
  {
     ByteArray := ComObjArray( 0x11, Rqd ) 
     DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",CRYPT_STRING_BASE64
         , "Ptr",NumGet( ComObjValue( ByteArray ) + 8 + A_PtrSize ), "UIntP",Rqd, "Int",0, "Int",0 )
  }

Return ByteArray
}
Pseudocode

Code: Select all

Base64ImageData := "//////////////"
ByteArray := Base64ToComByteArray( Base64ImageData )
If IsObject( ByteArray )
; do something
Demo: Include image in script and display it with WIA 2.0

Code: Select all

#NoEnv
#Warn
#SingleInstance, Force


Base64ImageData := "
( LTrim Join
  iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAAAz1BMVEX///8AAAD7/fwjdCv///+bwJ8SaBv///////9KjFBon26U
  u5j///////////////////////9Oj1VfmmX///////////////8NZRb4+PgJQw4MYBUJRw8MXRTP2dAKTBAJSBCguKJnf2nP1M+g
  raELWBNokmsLVBIKThEKUBEMWxMLUhILVhMJShDu8O7a4NotcTLk6eTE0cW4wrmUr5aHpolXh1uUopSGmIdWclhEfUgrVS3Ey8St
  wK6st614nHp3i3hCZES5ybosZDCUqpV0X6tXAAAAGHRSTlOzALL2jMz9TRXm3M6deVosJiHk35h1W1hTCYFzAAAIt0lEQVR42uzW
  SQr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ABgP6DBVhKBwjQAPa/uRpqAvRiLaR50gGw10JwAPHl4GABTF82QCgRYIcMcCKcrMsO
  SASgdwP0YTEIBAiHC7DqBQDaariI9NPlJ7jfFxEHSMcOQJABYsLJsQMA931lpg/PQ2AA+ugBbEwAhA+sDA6g7eMABaAAegQQSwZI
  CScxMsAE1j5aJrK/N/jGW44jAcAfyaEAwNejGTLAlgewQflDCH4RHnEeCsMPNNWxP7T0h7NBo2ofLyveVnWEJ0LwIYjz+mR5rEw5
  tyD04/W38W3/8pcp1DxzBsDEPll6Fd94IFo9kELNU3K1/o2uI+wSAwus9TLBQsPO6Vb9Zoh+jpC2TS417+plloU/evaXhoDmn/XD
  B+B+AM34e9aD97yuHz8fL2f+mV7GtGfgDggAaMbHOiF1kiz/ar6qHz/WR9bowPqrA0GL+kUANGMxz13GKKWMHY66ftY8dowoyA/f
  Hcj1Ks68zRurIgBFLD9wzMZr8nZYN48fYxHaZqMDTuBbWosIAhQ9iALbMas4duAjlM/pgN/oQNSyA4IARQxrMfPD+Xz+eRYKQvmA
  DkQCHeAAwGJ8RZMVgfYbAKOMAlAACkABKAAFoAAUgAJQAApAG2UUgAL41269oEAMwgAQnUStutZ+6FK8/0m3WIp32HTICR4k5AV4
  AV6AaxSmZrAJtAN8wTWDOZg7QIXcDJahdoAIpRmsQOwACdTgDjiF1AHEQ2jmCuDlBliBTzPWB1g7wNUJejRTHQqnPADigWDoDrgA
  eBkAuwe0ZGfgI5pcLgr4fQBcLRhrkTtE7jaPofwmA+ApxTorf5/ONSaRAWA88wA/FYbIdxzaJjsAAAAASUVORK5CYII=
)"

VectorImage := ComObjCreate( "WIA.Vector" )
VectorImage.BinaryData := Base64ToComByteArray( Base64ImageData ) 
Picture := VectorImage.Picture
VectorImage := "" 
Base64ImageData := ""
hBM := Picture.Handle 

Gui, Add, Picture,, HBITMAP:%hBM%
Gui, Show
Return ;    // end of auto-execcute section //


Base64ToComByteArray( ByRef B64 ) {  ; By SKAN / Created: 21-Aug-2017 / Topic: goo.gl/dyDxBN 
Static CRYPT_STRING_BASE64 := 0x1
Local  Rqd := 0, BLen := StrLen(B64), ByteArray := ""  

  If DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",CRYPT_STRING_BASE64
              , "UInt",0, "UIntP",Rqd, "Int",0, "Int",0 )
  {
     ByteArray := ComObjArray( 0x11, Rqd ) 
     DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",CRYPT_STRING_BASE64
         , "Ptr",NumGet( ComObjValue( ByteArray ) + 8 + A_PtrSize ), "UIntP",Rqd, "Int",0, "Int",0 )
  }

Return ByteArray
}

Other Links:
Scripts and Functions: WIA - Windows Image Acquisition (1.0.02.00 - 2015-05-03) by just me
Scripts and Functions: Base64 encoder/decoder for Binary data by SKAN
Other Utilities & Resources: WIA 2.0 for Windows XP (Windows Image Acquisition Automation Layer)
My Scripts and Functions: V1  V2
User avatar
joedf
Posts: 8949
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Base64ToComByteArray() :: Include image in script and display it with WIA 2.0

21 Aug 2017, 19:09

Hey skan!
I've bin trying to decode base64 encoded jpg files to the original binary data. But somehow the base64 decode based on crypt dll posted elsewhere was returning blank data. Do you think this could be related to running the ansi version instead of the u32 version? I haven't tried. I ended up using C for this.

Edit: Whoops! Looks like you posted a rewrite the day after my attempt :b
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Base64ToComByteArray() :: Include image in script and display it with WIA 2.0

24 Aug 2017, 04:23

joedf wrote:Edit: Whoops! Looks like you posted a rewrite the day after my attempt :b
HI joedf.
Is the rewrite working properly for you?
My Scripts and Functions: V1  V2
User avatar
joedf
Posts: 8949
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Base64ToComByteArray() :: Include image in script and display it with WIA 2.0

24 Aug 2017, 08:53

Just tested it (https://autohotkey.com/boards/viewtopic.php?t=35964). Works Flawlessly! :+1:
Very nice! :dance:
However, I am a little puzzled by this line here (in Base64Dec):
VarSetCapacity( Bin, 128 ), VarSetCapacity( Bin, 0 ), VarSetCapacity( Bin, Rqd, 0 )
Reading further in the docs, it seems this line is to force free the memory including unicode builds (128bytes). This is to ensure that you have a "clean" variable.
Am I correct with this assumption? :think:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Base64ToComByteArray() :: Include image in script and display it with WIA 2.0

24 Aug 2017, 09:24

joedf wrote:Just tested it (https://autohotkey.com/boards/viewtopic.php?t=35964). Works Flawlessly! :+1:
Very nice! :dance:
Thanks :)
joedf wrote:However, I am a little puzzled by this line here (in Base64Dec):
VarSetCapacity( Bin, 128 ), VarSetCapacity( Bin, 0 ), VarSetCapacity( Bin, Rqd, 0 )
Reading further in the docs, it seems this line is to force free the memory including unicode builds (128bytes). This is to ensure that you have a "clean" variable.
Am I correct with this assumption? :think:
Yes! In other words: Once the function returns, VarSetCapacity() can be used to find the binary data length.
:)
My Scripts and Functions: V1  V2
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Base64ToComByteArray() :: Include image in script and display it with WIA 2.0

25 Aug 2017, 07:01

Works great! I think I might use this in a few projects I'm working on.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat


Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gongnl and 73 guests