Jump to content
Sign in to follow this  
jorgev

Using a picture in hex format

Recommended Posts

jorgev

Hello,

I've converted an image to a hex string, now im trying to use this image inside for example a button.

Can't seem to find an example how i should proceed. Anyone can point me to an example? it doesnt have to be a button, can also just be an image that can be clicked.

here is some code to illustrate:

(ps. thankyou to the creator of _ImgProcess)

#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#include <GuiStatusBar.au3>

Example1()

Func Example1()
    Local $msg

    GUICreate("My GUI")

    $button = GUICtrlCreateButton("buttontext",10,10,100,100)
    $hexImage = _ImgProcess(Binary(bin()))

    ;How to use this image for the button?? Or how to use this image at all, display it in a gui


    GUISetState(@SW_SHOW)

    While 1
        $msg = GUIGetMsg()

        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete()
EndFunc

Func bin()
    Local _
    $bin = '0x89504E470D0A1A0A0000000D494844520000001C0000001C0802000000FD6F48C3000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA864000000D249444154484BD5963B0E02310C440347D89E92FB1F88929E2B642DC5DA8F331E0F681B5E43A48CDF3A38A0BDF5DEDBD5DCFDF352A2F4F378FA4A662E019D7EE585617C7CD19BC57C50707B79BF7C852025DE29AC27FDF226F6E3EBDEF258A7EF54F19646230E8A7B15A3817F51B018029BC0570A4667B218961AA5970452A941CAF82399F46798948C8B4F329596178004B054BC52590C4861D426038703C3519A19C3E2C85C729272E340F1EE52C53828BD2ED58D03EE8D7F28DB06316E6461307D43311A590C4845E30086FFE50DA5B515DAD77485880BAA0B0000000049454E44AE426082'
    $bin &= ''
    Return Binary($bin)
EndFunc

Func _ImgProcess(Const $bBinary);Merged code by Progandy and Zedna
    Local $iLen = BinaryLen($bBinary)
    If $iLen = 0 Then Return SetError(2, 0, 0)
    Local $hMem = _MemGlobalAlloc($iLen, $GMEM_MOVEABLE)
    If @error Or Not $hMem Then Return SetError(3, 0, 0)
    DllStructSetData(DllStructCreate("byte[" & $iLen & "]", _MemGlobalLock($hMem)), 1, $bBinary)
    If @error Then
        _MemGlobalUnlock($hMem)
        _MemGlobalFree($hMem)
        Return SetError(4, 0, 0)
    EndIf
    _MemGlobalUnlock($hMem)
    _GDIPlus_Startup()
    Local $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $hMem, "bool", True, "ptr*", 0)
    If @error Or $aResult[0] <> 0 Or $aResult[3] = 0 Then Return SetError(5, @error, 0)
    Local $hImage = DllCall($ghGDIPDll, "uint", "GdipLoadImageFromStream", "ptr", $aResult[3], "int*", 0)
    Local $error = @error
    Local $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
    DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $aResult[3], "dword", 8 + 8 * @AutoItX64, "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT))
    If @error Then Return SetError(6, $error, 0)
    If $hImage[2] = 0 Then Return SetError(7, 0, $hImage[2])
    Local $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage[2])
    _GDIPlus_ImageDispose($hImage[2])
    _GDIPlus_Shutdown()
    Return SetError(0, 0, $hBitmap)
EndFunc   ;==>_ImgProcess
Edited by jorgev

Share this post


Link to post
Share on other sites
UEZ

Try this:

#include <ButtonConstants.au3>
#include <Constants.au3>
#include <MemoryConstants.au3>
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#include <GuiStatusBar.au3>

Example1()

Func Example1()
    Local $msg

    GUICreate("My GUI")

    $button = GUICtrlCreateButton("buttontext",10,10,100,100)
    $hexImage = _ImgProcess(Binary(bin()))

    ;How to use this image for the button?? Or how to use this image at all, display it in a gui
    _WinAPI_DeleteObject(GUICtrlSendMsg($button, $BM_SETIMAGE, $IMAGE_BITMAP, $hexImage))

    GUISetState(@SW_SHOW)

    While 1
        $msg = GUIGetMsg()

        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete()
    _WinAPI_DeleteObject($hexImage)
EndFunc

Func bin()
    Local _
    $bin = '0x89504E470D0A1A0A0000000D494844520000001C0000001C0802000000FD6F48C3000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA864000000D249444154484BD5963B0E02310C440347D89E92FB1F88929E2B642DC5DA8F331E0F681B5E43A48CDF3A38A0BDF5DEDBD5DCFDF352A2F4F378FA4A662E019D7EE585617C7CD19BC57C50707B79BF7C852025DE29AC27FDF226F6E3EBDEF258A7EF54F19646230E8A7B15A3817F51B018029BC0570A4667B218961AA5970452A941CAF82399F46798948C8B4F329596178004B054BC52590C4861D426038703C3519A19C3E2C85C729272E340F1EE52C53828BD2ED58D03EE8D7F28DB06316E6461307D43311A590C4845E30086FFE50DA5B515DAD77485880BAA0B0000000049454E44AE426082'
    $bin &= ''
    Return Binary($bin)
EndFunc

Func _ImgProcess(Const $bBinary);Merged code by Progandy and Zedna
    Local $iLen = BinaryLen($bBinary)
    If $iLen = 0 Then Return SetError(2, 0, 0)
    Local $hMem = _MemGlobalAlloc($iLen, $GMEM_MOVEABLE)
    If @error Or Not $hMem Then Return SetError(3, 0, 0)
    DllStructSetData(DllStructCreate("byte[" & $iLen & "]", _MemGlobalLock($hMem)), 1, $bBinary)
    If @error Then
        _MemGlobalUnlock($hMem)
        _MemGlobalFree($hMem)
        Return SetError(4, 0, 0)
    EndIf
    _MemGlobalUnlock($hMem)
    _GDIPlus_Startup()
    Local $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $hMem, "bool", True, "ptr*", 0)
    If @error Or $aResult[0] <> 0 Or $aResult[3] = 0 Then Return SetError(5, @error, 0)
    Local $hImage = DllCall($ghGDIPDll, "uint", "GdipLoadImageFromStream", "ptr", $aResult[3], "int*", 0)
    Local $error = @error
    Local $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
    DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $aResult[3], "dword", 8 + 8 * @AutoItX64, "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT))
    If @error Then Return SetError(6, $error, 0)
    If $hImage[2] = 0 Then Return SetError(7, 0, $hImage[2])
    Local $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage[2])
    _GDIPlus_ImageDispose($hImage[2])
    _GDIPlus_Shutdown()
    Return SetError(0, 0, $hBitmap)
EndFunc   ;==>_ImgProcess

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
jorgev

UEZ,

Exactly what i needed. 

Much appriciated.

Jorgev

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×