Sign in to follow this  
Followers 0
MadaraUchiha

Bytes To Image

21 posts in this topic

Hi,

I made myself a little function:

$FileLoc = FileOpen("C:\Users\Domi\Desktop\arrow.png", 16)
$FileCode = FileRead($FileLoc)
FileClose($FileLoc)
MsgBox(0,'',$FileCode)

The result is a Binary String from the image.

How can I convert the BinaryString Back to an Image and make function wich returns the image, so

I can display it in a PictureBox? ;o


Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Checkout _GDIPlus_BitmapCreateFromMemory() function in current beta version.


Br,
UEZ

Edited by 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

Hey, thanks for your reply.

Can I also use _GDIPlus_BitmapCreateFromMemory() in the non-Beta-Version?

Is there a workarround?


Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Sure, either you extract the version from the beta or use this instead:

;==================================================================================================================================
; Name...........: _GDIPlus_BitmapFromMemory
; Description ...: Loads an image which is saved as a binary string and converts it to a bitmap (GDI+) or hbitmap (GDI)
; Syntax.........: _GDIPlus_BitmapFromMemory($bImage[, $hHBITMAP = False])
; Parameters ....: $bImage:    the binary string which contains any valid image which is supported by GDI+
;                  $hHBITMAP:  if false a bitmap will be created, if true a hbitmap (GDI) will be created
; Return values .: Success: handle to bitmap (GDI+ bitmap format) or hbitmap (WinAPI bitmap format)
;                  Otherwise fealure error codes:
;                  1: $bImage is not a binary string
;                  2: unable to create stream on HGlobal
;                  3: unable to create bitmap from stream
;                  4: unable to allocate moveable memory block
;                  5: unable to lock moveable memory block
;                  6: unable to unlock locked moveable memory block
;                  7: unable to release stream from memory
;                  8: unable to create (D)evice (I)ndepended (B)itmap
; Remarks .......: After you are done with the object, call _GDIPlus_BitmapDispose or _WinAPI_DeleteObject to release
;                  the object resources
; Author ........: UEZ
; Modified.......: progandy, Yashied
; Version........: v1.00 Build 2013-08-29 Beta
; Related .......: _GDIPlus_BitmapCreateFromStream, _WinAPI_BitmapCreateDIBFromBitmap, _GDIPlus_BitmapDispose, _WinAPI_DeleteObject
; Link ..........; @@MsdnLink@@ _GDIPlus_BitmapDispose _WinAPI_DeleteObject
; Example .......; No
;===================================================================================================================================
Func _GDIPlus_BitmapFromMemory($bImage, $hHBITMAP = False)
    If Not IsBinary($bImage) Then Return SetError(1, 0, 0)
    Local Const $memBitmap = Binary($bImage) ;load image  saved in variable (memory) and convert it to binary
    Local Const $iLen = BinaryLen($memBitmap) ;get binary length of the image
    Local $aResult = DllCall("kernel32.dll", "handle", "GlobalAlloc", "uint", 0x0002, "ulong_ptr", $iLen) ;allocates movable memory ($GMEM_MOVEABLE = 0x0002)
    If @error Then Return SetError(4, 0, 0)
    Local Const $hData = $aResult[0]
    $aResult = DllCall("kernel32.dll", "ptr", "GlobalLock", "handle", $hData)
    If @error Then Return SetError(5, 0, 0)
    Local Const $pData = $aResult[0] ;translate the handle into a pointer
    Local $tMem = DllStructCreate("byte[" & $iLen & "]", $pData) ;create struct
    DllStructSetData($tMem, 1, $memBitmap) ;fill struct with image data
    DllCall("kernel32.dll", "bool", "GlobalUnlock", "handle", $hData) ;decrements the lock count associated with a memory object that was allocated with GMEM_MOVEABLE
    If @error Then Return SetError(6, 0, 0)
    $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $hData, "int", True, "ptr*", 0) ;creates a stream object that uses an HGLOBAL memory handle to store the stream contents
    If @error Then Return SetError(2, 0, 0)
    Local Const $hStream = $aResult[3]
    $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0) ;creates a Bitmap object based on an IStream COM interface
    If @error Then Return SetError(3, 0, 0)
    Local Const $hBitmap = $aResult[2]
    $aResult = DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $hStream, "uint", 8 * (1 + @AutoItX64), "uint", 4, "uint", 23, "uint", 0, "ptr", 0, "ptr", 0, "str", "") ;release memory from $hStream to avoid memory leak
    If @error Then Return SetError(7, 0, 0)
    If $hHBITMAP Then
        Local Const $hHBmp = _WinAPI_BitmapCreateDIBFromBitmap($hBitmap) ;supports GDI transparent color format
        If @error Then
            _GDIPlus_BitmapDispose($hBitmap)
            Return SetError(8, 0, 0)
        EndIf
        Return $hHBmp
    EndIf
    Return $hBitmap
EndFunc   ;==>_GDIPlus_BitmapFromMemory

; #FUNCTION# ====================================================================================================================
; Name ..........: _WinAPI_BitmapCreateDIBFromBitmap
; Description ...: Creates a 32 bit GDI bitmap v5 using a GDI+ bitmap as source
; Syntax ........: _WinAPI_BitmapCreateDIBFromBitmap($hBitmap)
; Parameters ....: $hBitmap             - A handle to a GDI+ bitmap
; Return values .: success - A handle to a DIB bitmap, failure  - 0
; Remarks .......: After you are done with the object, call _WinAPI_DeleteObject to release the object resources
; Author ........: UEZ
; Modified ......:
; Related .......: _WinAPI_CreateDIBSection _GDIPlus_BitmapLockBits _GDIPlus_BitmapUnlockBits
; Link ..........: @@MsdnLink@@ CreateDIBSection SetBitmapBits
; Example .......: No
; ===============================================================================================================================
Func _WinAPI_BitmapCreateDIBFromBitmap($hBitmap)
    Local $tBIHDR, $aRet, $tData, $pBits, $hHBitmapv5 = 0
    $aRet = DllCall($ghGDIPDll, "uint", "GdipGetImageDimension", "handle", $hBitmap, "float*", 0, "float*", 0)
    If @error Or $aRet[0] Then Return 0
    $tData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $aRet[2], $aRet[3], $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    $pBits = DllStructGetData($tData, "Scan0")
    If Not $pBits Then Return 0
    $tBIHDR = DllStructCreate("dword bV5Size;long bV5Width;long bV5Height;word bV5Planes;word bV5BitCount;dword bV5Compression;" & _ ;http://msdn.microsoft.com/en-us/library/windows/desktop/dd183381(v=vs.85).aspx
            "dword bV5SizeImage;long bV5XPelsPerMeter;long bV5YPelsPerMeter;dword bV5ClrUsed;dword bV5ClrImportant;" & _
            "dword bV5RedMask;dword bV5GreenMask;dword bV5BlueMask;dword bV5AlphaMask;dword bV5CSType;" & _
            "int bV5Endpoints[3];dword bV5GammaRed;dword bV5GammaGreen;dword bV5GammaBlue;dword bV5Intent;" & _
            "dword bV5ProfileData;dword bV5ProfileSize;dword bV5Reserved")
    DllStructSetData($tBIHDR, "bV5Size", DllStructGetSize($tBIHDR))
    DllStructSetData($tBIHDR, "bV5Width", $aRet[2])
    DllStructSetData($tBIHDR, "bV5Height", $aRet[3])
    DllStructSetData($tBIHDR, "bV5Planes", 1)
    DllStructSetData($tBIHDR, "bV5BitCount", 32)
    DllStructSetData($tBIHDR, "bV5Compression", 0) ; $BI_BITFIELDS = 3, $BI_RGB = 0, $BI_RLE8 = 1, $BI_RLE4 = 2, $RGBA = 0x41424752
    DllStructSetData($tBIHDR, "bV5SizeImage", $aRet[3] * DllStructGetData($tData, "Stride"))
    DllStructSetData($tBIHDR, "bV5AlphaMask", 0xFF000000)
    DllStructSetData($tBIHDR, "bV5RedMask", 0x00FF0000)
    DllStructSetData($tBIHDR, "bV5GreenMask", 0x0000FF00)
    DllStructSetData($tBIHDR, "bV5BlueMask", 0x000000FF)
    DllStructSetData($tBIHDR, "bV5CSType", 2) ; $LCS_WINDOWS_COLOR_SPACE = 2
    DllStructSetData($tBIHDR, "bV5Intent", 4) ; $LCS_GM_IMA = 4
    $hHBitmapv5 = DllCall("gdi32.dll", "ptr", "CreateDIBSection", "hwnd", 0, "struct*", $tBIHDR, "uint", 0, "ptr*", 0, "ptr", 0, "dword", 0)
    If Not @error And $hHBitmapv5[0] Then
        DllCall("gdi32.dll", "dword", "SetBitmapBits", "ptr", $hHBitmapv5[0], "dword", $aRet[2] * $aRet[3] * 4, "ptr", DllStructGetData($tData, "Scan0"))
        $hHBitmapv5 = $hHBitmapv5[0]
    Else
        $hHBitmapv5 = 0
    EndIf
    _GDIPlus_BitmapUnlockBits($hBitmap, $tData)
    Return $hHBitmapv5
EndFunc   ;==>_WinAPI_BitmapCreateDIBFromBitmap

which is more or less equal.

 

Br,

UEZ

Edited by 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

Hm... Now I am confused... I just want to convert my BinaryStrign back to an Image.

But this won't work:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

_GDIPlus_Startup()
$Form1 = GUICreate("Test", 271, 240, 192, 124)
$Label1 = GUICtrlCreateLabel("Pfad", 8, 8, 44, 17)
$Input1 = GUICtrlCreateInput("", 56, 8, 121, 21)
$Button1 = GUICtrlCreateButton("...", 184, 8, 75, 25)
$Pic1 = GUICtrlCreatePic("", 8, 48, 252, 180)
$icon1 ='0x0000010001001010000001001800680300001600000028000000100000002000000001001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000DAE3E7242627909699D9E2E6000000000000000000000000000000000000000000000000000000000000000000000000DCE5E927292A101111969CA0DEE8EC000000000000000000000000000000000000000000000000000000000000000000DCE5E92E303100000017171892989BE3EDF1000000000000000000000000000000000000000000000000000000000000DCE5E92D2F30000000000000191A1B8F9497E6F0F4000000000000000000000000000000000000000000000000000000DCE5E92D2F300000000000000000001A1B1B8D9295E7F1F5000000000000000000000000000000000000000000000000DCE5E92D2F300000000000000000000000001819198D9295E7F0F4000000000000000000000000000000000000000000DCE5E92D2F300000000000000000000000000000000D0E0E787D7FD5DEE2000000000000000000000000000000000000DCE5E92D2F300000000000000000000000000000000D0E0E787D7FD5DEE2000000000000000000000000000000000000DCE5E92D2F300000000000000000000000001819198D9295E7F0F4000000000000000000000000000000000000000000DCE5E92D2F300000000000000000001A1B1B8D9295E7F1F5000000000000000000000000000000000000000000000000DCE5E92D2F30000000000000191A1B8F9497E6F0F4000000000000000000000000000000000000000000000000000000DCE5E92E303100000017171892989BE3EDF1000000000000000000000000000000000000000000000000000000000000DCE5E927292A101111969CA0DEE8EC000000000000000000000000000000000000000000000000000000000000000000DAE3E7242627909699D9E2E6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFF9C41E1FF9C41E0FF9C41E47F9C41E63F9C41E71F9C41E78F9C41E7C79C41E7C79C41E78F9C41E71F9C41E63F9C41E47F9C41E0FF9C41E1FF9C41FFFF9C41'


GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _GDIPlus_Shutdown()
            Exit
        Case $Button1
            DisplayImage($Pic1,_GDIPlus_BitmapFromMemory($icon1))
    EndSwitch
WEnd


Func _GDIPlus_BitmapFromMemory($bImage, $hHBITMAP = False)
    If Not IsBinary($bImage) Then Return SetError(1, 0, 0)
    Local Const $memBitmap = Binary($bImage) ;load image  saved in variable (memory) and convert it to binary
    Local Const $iLen = BinaryLen($memBitmap) ;get binary length of the image
    Local $aResult = DllCall("kernel32.dll", "handle", "GlobalAlloc", "uint", 0x0002, "ulong_ptr", $iLen) ;allocates movable memory ($GMEM_MOVEABLE = 0x0002)
    If @error Then Return SetError(4, 0, 0)
    Local Const $hData = $aResult[0]
    $aResult = DllCall("kernel32.dll", "ptr", "GlobalLock", "handle", $hData)
    If @error Then Return SetError(5, 0, 0)
    Local Const $pData = $aResult[0] ;translate the handle into a pointer
    Local $tMem = DllStructCreate("byte[" & $iLen & "]", $pData) ;create struct
    DllStructSetData($tMem, 1, $memBitmap) ;fill struct with image data
    DllCall("kernel32.dll", "bool", "GlobalUnlock", "handle", $hData) ;decrements the lock count associated with a memory object that was allocated with GMEM_MOVEABLE
    If @error Then Return SetError(6, 0, 0)
    $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $hData, "int", True, "ptr*", 0) ;creates a stream object that uses an HGLOBAL memory handle to store the stream contents
    If @error Then Return SetError(2, 0, 0)
    Local Const $hStream = $aResult[3]
    $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0) ;creates a Bitmap object based on an IStream COM interface
    If @error Then Return SetError(3, 0, 0)
    Local Const $hBitmap = $aResult[2]
    $aResult = DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $hStream, "uint", 8 * (1 + @AutoItX64), "uint", 4, "uint", 23, "uint", 0, "ptr", 0, "ptr", 0, "str", "") ;release memory from $hStream to avoid memory leak
    If @error Then Return SetError(7, 0, 0)
    If $hHBITMAP Then
        Local Const $hHBmp = _WinAPI_BitmapCreateDIBFromBitmap($hBitmap) ;supports GDI transparent color format
        If @error Then
            _GDIPlus_BitmapDispose($hBitmap)
            Return SetError(8, 0, 0)
        EndIf
        Return $hHBmp
    EndIf
    Return $hBitmap
EndFunc  


Func _WinAPI_BitmapCreateDIBFromBitmap($hBitmap)
    Local $tBIHDR, $aRet, $tData, $pBits, $hHBitmapv5 = 0
    $aRet = DllCall($ghGDIPDll, "uint", "GdipGetImageDimension", "handle", $hBitmap, "float*", 0, "float*", 0)
    If @error Or $aRet[0] Then Return 0
    $tData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $aRet[2], $aRet[3], $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    $pBits = DllStructGetData($tData, "Scan0")
    If Not $pBits Then Return 0
    $tBIHDR = DllStructCreate("dword bV5Size;long bV5Width;long bV5Height;word bV5Planes;word bV5BitCount;dword bV5Compression;" & _ ;http://msdn.microsoft.com/en-us/library/windows/desktop/dd183381(v=vs.85).aspx
            "dword bV5SizeImage;long bV5XPelsPerMeter;long bV5YPelsPerMeter;dword bV5ClrUsed;dword bV5ClrImportant;" & _
            "dword bV5RedMask;dword bV5GreenMask;dword bV5BlueMask;dword bV5AlphaMask;dword bV5CSType;" & _
            "int bV5Endpoints[3];dword bV5GammaRed;dword bV5GammaGreen;dword bV5GammaBlue;dword bV5Intent;" & _
            "dword bV5ProfileData;dword bV5ProfileSize;dword bV5Reserved")
    DllStructSetData($tBIHDR, "bV5Size", DllStructGetSize($tBIHDR))
    DllStructSetData($tBIHDR, "bV5Width", $aRet[2])
    DllStructSetData($tBIHDR, "bV5Height", $aRet[3])
    DllStructSetData($tBIHDR, "bV5Planes", 1)
    DllStructSetData($tBIHDR, "bV5BitCount", 32)
    DllStructSetData($tBIHDR, "bV5Compression", 0) ; $BI_BITFIELDS = 3, $BI_RGB = 0, $BI_RLE8 = 1, $BI_RLE4 = 2, $RGBA = 0x41424752
    DllStructSetData($tBIHDR, "bV5SizeImage", $aRet[3] * DllStructGetData($tData, "Stride"))
    DllStructSetData($tBIHDR, "bV5AlphaMask", 0xFF000000)
    DllStructSetData($tBIHDR, "bV5RedMask", 0x00FF0000)
    DllStructSetData($tBIHDR, "bV5GreenMask", 0x0000FF00)
    DllStructSetData($tBIHDR, "bV5BlueMask", 0x000000FF)
    DllStructSetData($tBIHDR, "bV5CSType", 2) ; $LCS_WINDOWS_COLOR_SPACE = 2
    DllStructSetData($tBIHDR, "bV5Intent", 4) ; $LCS_GM_IMA = 4
    $hHBitmapv5 = DllCall("gdi32.dll", "ptr", "CreateDIBSection", "hwnd", 0, "struct*", $tBIHDR, "uint", 0, "ptr*", 0, "ptr", 0, "dword", 0)
    If Not @error And $hHBitmapv5[0] Then
        DllCall("gdi32.dll", "dword", "SetBitmapBits", "ptr", $hHBitmapv5[0], "dword", $aRet[2] * $aRet[3] * 4, "ptr", DllStructGetData($tData, "Scan0"))
        $hHBitmapv5 = $hHBitmapv5[0]
    Else
        $hHBitmapv5 = 0
    EndIf
    _GDIPlus_BitmapUnlockBits($hBitmap, $tData)
    Return $hHBitmapv5
EndFunc   

Func DisplayImage($id, $file)
    Local Const $IMAGE_BITMAP = 0
    Local Const $STM_SETIMAGE = 0x0172
    Local $hImage = _GDIPlus_ImageLoadFromFile($file)
    If @error Then Return SetError(1, 0, 0)
    Local $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject(GUICtrlSendMsg($id, $STM_SETIMAGE, $IMAGE_BITMAP, $hHBitmap))
    _WinAPI_DeleteObject($hHBitmap)
EndFunc

Share this post


Link to post
Share on other sites

When you want to load an icon from the memory and use it afterwards you have to convert the bitmap to an icon using _GDIPlus_HICONCreateFromBitmap function.

Checkout File to Base64 String Code Generator topic for examples how to use embedded files in GDI+.
 

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

I already saw your Converter and I like to code a similar thing but using Binary Instead of Base64 ;)

Anyways: I got it:

;http://ipv4.autoit.de/index.php?page=Thread&threadID=30707
;http://www.autoitscript.com/forum/topic/71007-set-image-string-directly-to-control/#entry873391

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#include <Memory.au3>

Global Const $IMAGE_BITMAP = 0
Global Const $STM_SETIMAGE = 0x0172
Global $msg
Global Const $hGUI = GUICreate("Test", 600, 250)
Global Const $idLogo = GUICtrlCreatePic("", 215, 20, 169, 68)
Global Const $idButton = GUICtrlCreateButton("", 266, 150, 78 , 81, $BS_BITMAP)
Global Const $hButton = GUICtrlGetHandle($idButton)
GUISetState()

$Image = '0x89504E470D0A1A0A0000000D49484452000000100000001008060000001FF3FF610000001974455874536F6674776172650041646F626520496D616765526561647971C9653C000001AF4944415478DAA453BF4BC34014FE52D3A885621D4470E8A2B82A1C0E6E4E227670D241BA38B9F64F707474112C8820C649110771E8E2E024580705051507B75ADA6AFA2BC95D2EDE5D6B8CB582D00F5EDEBDCBBDEFFDBAD37CDF472FD0E5676AF30E9AA6059B899191602DF6B3425D32C64C4629A8E328716D1B0F1B332D824ED4DEDF31188F7F99A42D1266E7D948370219A961594A4B492F8E11A133F0FDF4BF08243CC6E0D4EB2ADD8F0F0FE9549250D7FD45A24A108DCC86D2FC26F13C25B6CD502830ACA6C6C9FEC97DA6DD7833201051C8DACA34A9D518E4BFAFC9B4D640A5E20A120F4F4F14CB0B93E4F0F436205104B2A39645512C3A41745794506D34D1683641451F642612CFAF7D989B4D92DCC563E69B401C328C0812090394FB78B329CA9C83C50CF0C1282C11BDE6704530311AC3D9F94D5E8C772B5C427E3B9BFBF3B20C2DCD93428363622882ABE39C728EF6F79BE126AE73CE7F39CA3AC534AE4BFA0012C3C0CBC151CBD930CC1F53E8062E6A764469127171494BBBA672D643CE7F1278A269F56A35B0CB3B7B2DE768D4ECFA163A61552A61332FDF429FAE9BDDCE6ABDBEC6087AC4A700030056B4E792141E86110000000049454E44AE426082'



Func DrawIcon($ImageString)
Global Const $Bmp_Button = Load_BMP_From_Mem(Binary($ImageString()), True)
_WinAPI_DeleteObject(_SendMessage($hButton, $BM_SETIMAGE, $IMAGE_BITMAP, $Bmp_Button))
_WinAPI_UpdateWindow($hButton)
GUISetState()
_WinAPI_DeleteObject($Bmp_Button)
EndFunc

While True
    $msg = GUIGetMsg()
    Switch $msg
        Case $idButton
            DrawIcon($Image)
        Case $GUI_EVENT_CLOSE
            GUIDelete($hGUI)
            Exit
    EndSwitch
WEnd

Func Load_BMP_From_Mem($bImage, $hHBITMAP = False)
    If Not IsBinary($bImage) Then Return SetError(1, 0, 0)
    Local $declared = True
    If Not $ghGDIPDll Then
        _GDIPlus_Startup()
        $declared = False
    EndIf
    Local $aResult
    Local Const $memBitmap = Binary($bImage) ;load image  saved in variable (memory) and convert it to binary
    Local Const $len = BinaryLen($memBitmap) ;get length of image
    Local Const $hData = _MemGlobalAlloc($len, $GMEM_MOVEABLE) ;allocates movable memory  ($GMEM_MOVEABLE = 0x0002)
    Local Const $pData = _MemGlobalLock($hData) ;translate the handle into a pointer
    Local $tMem = DllStructCreate("byte[" & $len & "]", $pData) ;create struct
    DllStructSetData($tMem, 1, $memBitmap) ;fill struct with image data
    _MemGlobalUnlock($hData) ;decrements the lock count  associated with a memory object that was allocated with GMEM_MOVEABLE
    $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $pData, "int", True, "ptr*", 0) ;Creates a stream object that uses an HGLOBAL memory handle to store the stream contents
    If @error Then SetError(2, 0, 0)
    Local Const $hStream = $aResult[3]
    $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0) ;Creates a Bitmap object based on an IStream COM interface
    If @error Then SetError(3, 0, 0)
    Local Const $hBitmap = $aResult[2]
    Local $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
    DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $hStream, "dword", 8 + 8 * @AutoItX64, _
                                           "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT)) ;release memory from $hStream to avoid memory leak
    $tMem = 0
    $tVARIANT = 0
    If $hHBITMAP Then
        Local Const $hHBmp = _GDIPlus_BitmapCreateDIBFromBitmap($hBitmap)
        _GDIPlus_BitmapDispose($hBitmap)
        If Not $declared Then _GDIPlus_Shutdown()
        Return $hHBmp
    EndIf
    If Not $declared Then _GDIPlus_Shutdown()
    Return $hBitmap
EndFunc   ;==>Load_BMP_From_Mem

Func _GDIPlus_BitmapCreateDIBFromBitmap($hBitmap)
    Local $tBIHDR, $Ret, $tData, $pBits, $hResult = 0
    $Ret = DllCall($ghGDIPDll, 'uint', 'GdipGetImageDimension', 'ptr', $hBitmap, 'float*', 0, 'float*', 0)
    If (@error) Or ($Ret[0]) Then Return 0
    $tData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $Ret[2], $Ret[3], $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    $pBits = DllStructGetData($tData, 'Scan0')
    If Not $pBits Then Return 0
    $tBIHDR = DllStructCreate('dword;long;long;ushort;ushort;dword;dword;long;long;dword;dword')
    DllStructSetData($tBIHDR, 1, DllStructGetSize($tBIHDR))
    DllStructSetData($tBIHDR, 2, $Ret[2])
    DllStructSetData($tBIHDR, 3, $Ret[3])
    DllStructSetData($tBIHDR, 4, 1)
    DllStructSetData($tBIHDR, 5, 32)
    DllStructSetData($tBIHDR, 6, 0)
    $hResult = DllCall('gdi32.dll', 'ptr', 'CreateDIBSection', 'hwnd', 0, 'ptr', DllStructGetPtr($tBIHDR), 'uint', 0, 'ptr*', 0, 'ptr', 0, 'dword', 0)
    If (Not @error) And ($hResult[0]) Then
        DllCall('gdi32.dll', 'dword', 'SetBitmapBits', 'ptr', $hResult[0], 'dword', $Ret[2] * $Ret[3] * 4, 'ptr', DllStructGetData($tData, 'Scan0'))
        $hResult = $hResult[0]
    Else
        $hResult = 0
    EndIf
    _GDIPlus_BitmapUnlockBits($hBitmap, $tData)
    Return $hResult
EndFunc   ;==>_GDIPlus_BitmapCreateDIBFromBitmap

Share this post


Link to post
Share on other sites

It is also binary but Base64 encoded. ;)

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

Is there an easy way to set an icon to a button, but without extracting the image?

I mean if I load an Image with FileInstall() it gets extracted at runtime? Can't I compile it directly into the exe with a few lines of code?

In C# I can set it as Ressource and the set it in the properties to the controls icon.

I don't wont large functions because they maybe won't work on all systems ;)


Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Well, if you call a function within your script you think it is not large? You have to look "backstage" to see how large some functions really are. ;)

The good thing is that AutoIt hides all the large functions and makes it easy to understand and use it.

To use an icon without extracting it first to the hd, you have to convert the icon to a GDI bitmap and send it to the button control. Might be also other ways to do it.

Br,

UEZ

Edited by 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

Well, if you call a function within your script you think it is not large? You have to look "backstage" to see how large some functions really are. ;)

The good thing is that AutoIt hides all the large functions and makes it easy to understand and use it.

To use an icon without extracting it first to the hd, you have to convert the icon to a GDI bitmap and send it to the button control. Might be also other ways to do it.

Br,

UEZ

Can you give me a sample how I can do this?

Is the Icon stored in the exe then, and doesn't matter on which PC the .exe gets executed, "the icon is always in th pocket", and I can display it without extracting any files? :)


Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Here an example:

 

#AutoIt3Wrapper_Version=b
#include <ButtonConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

If FileGetVersion(@AutoItExe) < "3.3.9.21" Then Exit MsgBox(16, "Error", "AutoIt version 3.3.9.21 or higher needed!", 30)

$bIcon = Binary("0x000001000100303010000100040068060000160000002800000030000000600000000100040000000000000000000000000000000000000000000000000002020100493B2E00694D320068615B0086603C0096735200AA856300CD9660007E808100A9A8A600CFB49A00D7C4B100BDBEC000BFC0C100D8D9D90000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8830FFFFFFFFFFF113138833331FFFFFFFFFFFFFFFFFFFF199830FFFFFFFF193999998999831111FFFFFFFFFFFFFFFF3999930FFFFFF8899999CCC9C931199131FFFFFFFFFFFFFF3C999910FF1839999995444533838C9998FFFFFFFFFFFFFFF9999981F139999952224444229119D9998FFFFFFFFFFFFFF9C9999803C99952244454555893014CD9981FFFFFFFFFFFFFDC9999139982244575767756C811126E9981FFFFFFFFFFFF8D9999818822455677777766C9312425E9991FFFFFFFFFFFFC99C9931144576777777777C99325425B9C3FFFFFFFFFFFF9D999C811456777777777779C93274428E991FFFFFFFFFFFFEC9C9C8126777777777777AC93156442AC991FFFFFFFFFFF8E9C9C93167777777777779D991477523DCD3FFFFFFFFFFF8CDCC9C912777777777777ACCC34764427DC9FFFFFFFFFF1C9EDCCCC814777777777779D9C82777523D9C3FFFFFFFFF19C9EBDDDC8147777777777ADCC82777442AD81FFFFFFFFF3DB88EDDDDD8114677777779EDD817776528E91FFFFFFFFF19D826EDDDDD983114456769DDD917775444DD9FFFFFFFFF1CD325EEDDDDDDC98331212CDDD926777544DD91FFFFFFF1CDD3256EBEDEDDEDDECC999DDDEC34777542DD91FFFFFFF1DD94255AEBDEDEDEDDDEDEDEDEDD34777542DD8FFFFFFFF19DC32477AEEEBEBEEEEEBEDEDEDE82776442CDDFFFFFFFFF3ED344577BEEEED999DDEEEEEEDE91777544DE8FFFFFFFFF3ED3246777BEEEE82357ADEEEEEEC1576544DE3FFFFFFFFF3EE8245777ABEEE928376CEEEEEEE3576428DE8FFFFFFFFF3ED922577AA7EEEE81349EEEEEEEE3675429EE9FFFFFFFFF1EED3246A7AA7EEED818EEEEEEEEC3A7542CE91FFFFFFFFFF8EE8226A7AAAAEEEEEEEEEEEEEEE377428EE8FFFFFFFFFFFF8EC325AAAAAABEEEEEEEEEEEEEC3A942DEE8FFFFFFFFFFFF1DE9159AAAABABEEEEEEEEEEEE95A628EEE8FFFFFFFFFFFF3EEE326AAAABBBEEEEEEEEEEEE3A948EEEED81FFFFFFFFFFF1EEE359AAABBBAEEEEEEEEEE86B63CEEEEEEE8FFFFFFFFFFF8EED35CAAABBBBEEEEEEEE98B95EEEEEEEEEE2FFFFFFFFFFF9EEE88ABCBBBBBEEEEEEE8A68DEEEE98EEEE9FFFFFFFFFFF3EEEE836AEDBBBBEEEED9A89EEEEE9988EEE9FFFFFFFFFFFF28EEEE9386ABBBDC96858DEEEEEEDEE8EEEE1FFFFFFFFFFFFF39EEEE98833535889EEEEEEEEEE9D9EEEE3FFFFFFFFFFFFFFF8EEEEEEEEEEEEEEEEEEEEEEEE83EEEEE1FFFFFFFFFFFFFFF183EEEEEEEEEEEEEE323EEEEEE89EEE9FFFFFFFFFFFFFFFFFFF3399EE89EED133FFF8EEEE888EEE1FFFFFFFFFFFFFFFFFFFFFF131FF811FFFFFF1EEEEEDEEECFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9EEEEE9C1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF38D801FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDFFFFFFFFFFFFFFF87FF000FFFFFFFFF03FC0000FFFFFFFF01F800003FFFFFFF00C000003FFFFFFF808000001FFFFFFF8000000007FFFFFFC000000003FFFFFFC000000001FFFFFFE000000001FFFFFFE000000000FFFFFFF0000000007FFFFFF0000000007FFFFFF0000000007FFFFFE0000000003FFFFFE0000000003FFFFFE0000000003FFFFFE0000000003FFFFFE0000000001FFFFFC0000000001FFFFFC0000000003FFFFFC0000000003FFFFFE0000000003FFFFFE0000000003FFFFFE0000000003FFFFFE0000000003FFFFFE0000000003FFFFFF0000000007FFFFFF8000000007FFFFFF8000000007FFFFFF8000000001FFFFFFC000000000FFFFFFE0000000007FFFFFF0000000007FFFFFF0000000007FFFFFF8000000003FFFFFFE000000003FFFFFFF800000003FFFFFFF800000007FFFFFFFF00038007FFFFFFFFE31F800FFFFFFFFFFFFFE00FFFFFFFFFFFFFF03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")

_GDIPlus_Startup()
$hHBitmap = _GDIPlus_BitmapCreateFromMemory($bIcon, True) ;create GDI bitmap from icon 48x48
$hGUI = GUICreate("", 130, 120)
$iButton = GUICtrlCreateButton("GDI+", 21, 21, 88, 78)
_WinAPI_DeleteObject(GUICtrlSendMsg($iButton, $BM_SETIMAGE, $IMAGE_BITMAP, $hHBitmap))
GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_WinAPI_DeleteObject($hHBitmap)
_GDIPlus_Shutdown()
GUIDelete()
Exit

Requires the beta or you have to embed the beta functions manually to your script.

 

 

Br,

UEZ

Edited by 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

#13 ·  Posted (edited)

Yes that works...

But let me explain you my problem in detail.

I want a custom image file appear in a Button. Lets call it MyFile = 'C:MyFile.png'

I want MyFile be the icon of my button.

The icon must be:

1) saved/embed in the exe, so no path to file at runtime is needed.

2) Display it without extracting it (No FileInstall())

3) If possible support .png format but .ICO would also be okay.

4) Support both. Text and Icon on the button.

Ideas?

Edited by MadaraUchiha

Share this post


Link to post
Share on other sites

MadaraUchiha,

I cannot follow you. Did you compile the script and try to run it on a different pc? You can use any image format which is supported by GDI+.

Btw, if you remove $BS_BITMAP, you can add a text to the buttom.

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

Hm, look, your example works fine for Win Vista, 7, 8.

lw4vbuys.png

But on Windows XP, it won't work:

omgxvx88.png

Maybe I can add the Icon as a Ressource and then load it to the Control (Button).

So i can display the icon also on Windows XP?

And without extracting it?


Share this post


Link to post
Share on other sites

You can use $BS_BITMAP in WinXP but then the button text is gone. I'm not using WinXP since a long time and for me it is an obsolete os.

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

Hm, and what about using ressources to set an Icon to a button? I sthis possible? Then it will also work without extracting, won't it? :o


Share this post


Link to post
Share on other sites

Just something like this: 

#include <GDIPlus.au3>
#include <GuiMenu.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <WinAPIEx.au3>
#include <WindowsConstants.au3>
#Include <Constants.au3>
Global $iMemo


$file = FileOpenDialog("Select ICO file", @ScriptDir, ("Icons (*.ico)"))

If @error Then Exit

$Ret = DllCall("shell32", "long", "ExtractAssociatedIcon", "int", 0, "str", $file, "int*", 0)
$hIcon = $Ret[0]

_GDIPlus_Startup()
$hImage = DllCall($ghGDIPDll,"int", "GdipCreateBitmapFromHICON", "ptr", $hIcon, "int*", 0)
$hImage = $hImage[2]
_WinAPI_DestroyIcon($hIcon)

$dest_x = 16
$dest_y = 16

$hBitmap = _GDIPlus_BitmapCreateFromScan0($dest_x, $dest_y)
$hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $dest_x, $dest_y)
$hIcon = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)

_Main()

_GDIPlus_GraphicsDispose($hContext)
_WinAPI_DeleteObject($hIcon)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()

Exit


Func _Main()
    Local $hGUI, $hFile, $hEdit, $hHelp, $hMain
    Local Enum $idNew = 1000, $idOpen, $idSave, $idExit, $idCut, $idCopy, $idPaste, $idAbout

    ; Create GUI
    $hGUI = GUICreate("Menu", 400, 300)

    ; Create File menu
    $hFile = _GUICtrlMenu_CreateMenu ()
    _GUICtrlMenu_InsertMenuItem ($hFile, 0, "&New", $idNew)
    _GUICtrlMenu_InsertMenuItem ($hFile, 1, "&Open", $idOpen)
    _GUICtrlMenu_InsertMenuItem ($hFile, 2, "&Save", $idSave)
    _GUICtrlMenu_InsertMenuItem ($hFile, 3, "", 0)
    _GUICtrlMenu_InsertMenuItem ($hFile, 4, "E&xit", $idExit)

    ; Create Edit menu
    $hEdit = _GUICtrlMenu_CreateMenu ()
    _GUICtrlMenu_InsertMenuItem ($hEdit, 0, "&Cut", $idCut)
    _GUICtrlMenu_InsertMenuItem ($hEdit, 1, "C&opy", $idCopy)
    _GUICtrlMenu_InsertMenuItem ($hEdit, 2, "&Paste", $idPaste)

    ; Create Help menu
    $hHelp = _GUICtrlMenu_CreateMenu ()
    _GUICtrlMenu_InsertMenuItem ($hHelp, 0, "&About", $idAbout)

    ; Create Main menu
    $hMain = _GUICtrlMenu_CreateMenu ()
    _GUICtrlMenu_InsertMenuItem ($hMain, 0, "&File", 0, $hFile)
    _GUICtrlMenu_InsertMenuItem ($hMain, 1, "&Edit", 0, $hEdit)
    _GUICtrlMenu_InsertMenuItem ($hMain, 2, "&Help", 0, $hHelp)

    ; Set window menu
    _GUICtrlMenu_SetMenu ($hGUI, $hMain)

    ; Create memo control
    $iMemo = GUICtrlCreateEdit("", 2, 2, 396, 276, 0)
    GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
    GUISetState()

    ; Set New menu item to have a bitmap
    _GUICtrlMenu_SetItemBmp ($hFile, 0, $hIcon)
    MemoWrite("Item bitmap handle: 0x" & Hex(_GUICtrlMenu_GetItemBmp ($hFile, 0)))

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc ;==>_Main

; Write message to memo
Func MemoWrite($sMessage)
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc ;==>MemoWrite

Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    $GDIP_STATUS = $aResult[0]
    Return $aResult[6]
EndFunc ;==>_GDIPlus_BitmapCreateFromScan0

But instead of showing the icon in a ContextMenu, it has to show up in a Button, and also it should be stroed in the executable, so no FileInstall() is needed. (Saved permanently, not temporary!)


Share this post


Link to post
Share on other sites

Also, It seems like png images are not weill liked in Autoit (not supported for icons?)


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  
Followers 0