Jump to content

Hex to Image in Memory


 Share

Recommended Posts

Yo,

I have a Hex String of an Image on my Server.

I like to download the Hex String using InetRead and then convert it in Memory and display it on my Form, without having

any files on the HDD laying arround.

I uplaoded a test Hex String to my Server, which is an Image (As HexString), you can use it to test.

I tried it this way, but it doesn't work:

#include <ButtonConstants.au3>
#include <GDIplus.au3>
#Include <Memory.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Download Hex Image", 615, 425, 192, 124)
$Button1 = GUICtrlCreateButton("Get", 16, 8, 75, 25)
$Pic1 = GUICtrlCreatePic("", 16, 40, 588, 364)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $x = InetRead("http://nexno.bplaced.net/Hex.txt")
            GUICtrlSetData($Pic1,Load_BMP_From_Mem($x))
    EndSwitch
WEnd

Func Load_BMP_From_Mem($mem_image, $hHBITMAP = False)
    If Not IsBinary($mem_image) Then Return SetError(1, 0, 0)
    Local $declared = True
    If Not $ghGDIPDll Then
        _GDIPlus_Startup()
        $declared = False
    EndIf
    Local Const $memBitmap = Binary($mem_image) ;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
    Local $hStream = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $pData, "int", True, "ptr*", 0)
    $hStream = $hStream[3]
    Local $hBitmap = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0) ;Creates a Bitmap object based on an IStream COM interface
    $hBitmap = $hBitmap[2]
    Local Const $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
    If $hHBITMAP Then
        Local Const $hHBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
        _GDIPlus_BitmapDispose($hBitmap)
        If Not $declared Then _GDIPlus_Shutdown()
        Return $hHBmp
    EndIf
    If Not $declared Then _GDIPlus_Shutdown()
    Return $hBitmap
EndFunc

Its loading a few seconds then nothing happens. No errors, but also no Image.

Can someone help me out? :)

Link to comment
Share on other sites

Beside that you cannot set a bitmap with GUICtrlSetData() to the pic control!
 

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            If $hB Then _WinAPI_DeleteObject($hB)
            Exit
        Case $Button1
            $x = InetRead("http://nexno.bplaced.net/Hex.txt")
            $hB = Load_BMP_From_Mem(Binary("0x" & BinaryToString($x)), 1)
            _WinAPI_DeleteObject(GUICtrlSendMsg($Pic1, $STM_SETIMAGE, $IMAGE_BITMAP, $hB))
    EndSwitch
WEnd

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Beside that you cannot set a bitmap with GUICtrlSetData() to the pic control!

 

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            If $hB Then _WinAPI_DeleteObject($hB)
            Exit
        Case $Button1
            $x = InetRead("http://nexno.bplaced.net/Hex.txt")
            $hB = Load_BMP_From_Mem(Binary("0x" & BinaryToString($x)), 1)
            _WinAPI_DeleteObject(GUICtrlSendMsg($Pic1, $STM_SETIMAGE, $IMAGE_BITMAP, $hB))
    EndSwitch
WEnd

Br,

UEZ

In welchem Include File ist $STM_SETIMAGE definiert, ich kann das nicht finden?

Ok, hab se:

Global Const $STM_SETIMAGE = 0x0172
Edited by MadaraUchiha

Link to comment
Share on other sites

_GDIPlus_BitmapCreateFromMemory() and Load_BMP_From_Mem() are more or less the same functions. ;)

For _GDIPlus_BitmapCreateFromMemory() you need the latest AutoIt version.

 

If you minimize the GUI and restore it you will see that the image in the pic control has been resized properly. 

 

I tried _WinAPI_RedrawWindow() and _WinAPI_UpdateWindow() to simulate the restore and thus the repaint of the GUI but it doesn't work. Maybe somebody has a solution for this...

Another methode is to resize the image before sending to the pic control using e.g. _GDIPlus_ImageResize() from the current release.

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <GDIPlus.au3>
#Include <Memory.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


_GDIPlus_Startup()
Global Const $STM_SETIMAGE = 0x0172
Global $hB
$Form1 = GUICreate("Download Hex Image", 615, 425, 192, 124)
$Button1 = GUICtrlCreateButton("Get", 16, 8, 75, 25)
$Pic1 = GUICtrlCreatePic("", 16, 40, 588, 364)
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            If $hB Then _WinAPI_DeleteObject($hB)
            _GDIPlus_Shutdown()
            Exit
        Case $Button1
            $x = InetRead("http://nexno.bplaced.net/Hex.txt")
            $hB1 = _GDIPlus_BitmapCreateFromMemory(Binary("0x" & BinaryToString($x)))
            $hB2 = _GDIPlus_ImageResize($hB1, 588, 364)
            $hB = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hB2)
            _WinAPI_DeleteObject(GUICtrlSendMsg($Pic1, $STM_SETIMAGE, $IMAGE_BITMAP, $hB))
            _GDIPlus_BitmapDispose($hB1)
            _GDIPlus_BitmapDispose($hB2)
    EndSwitch
WEnd

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Hello UEZ,

First thanks for the quick reply ;)

Your code works but with a little bug which I don't understand. If I start it first time, and hit the button it works fine.

If I then change the hex-image-file on server, and hit button again it still shows the old image. 

Even if I close and restart the app it shows the old one.

After a reboot of my pc it shows the new one when starting the app again and hitting the button.

Is the image still in memory.

How can it always load the new image when hitting the button?

Like a force-reload-image or something like this? ;)

Link to comment
Share on other sites

Use InetRead with the $INET_FORCERELOAD option set. It's documented in the help file.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...